Jump to content

Overwrite uploaded photo


graham23s

Recommended Posts

Hi Guys,

 

i;m using this code to upload photos it works great basic but great, what i want to do is when a user uploads a photo the previuos one is deleted, i tried a few unlink codes but it never workjed out, can anyone give me some help

 

code:

 

<?php
     // For register_global on PHP settings//////////////////////////////////////////////
     $member = $_COOKIE['member'];

     session_start(); // you must put this to read session variables/////////////////////

     if (empty($member) || !isset($member)) // fail to read the browser cookie///////////
     {
     // Try to read session
     if (empty($_SESSION['member']) || !isset($_SESSION['member']))
     {
           header("Location: login.php"); // redirect user to login//////////////////////
           exit;
     } else {
          $member = $_SESSION['member'];
  }
}
     //Includes... //////////////////////////////////////////////////////////////////////
     include("includes/db_connection.php");
     include("includes/constants.php");
     include("includes/header.php");
     include("includes/loginnav.php");
?>
<p align="left">Welcome, <b><font color="red"><? echo $_COOKIE['member'] ?></font></b>! (<a href="logout.php">Logout</a>) - [ <a href="change_password.php">Change Password Or E-mail Address</a> ]-[ <a href="edit_profile.php">Edit Profile</a> ]-[ <a href="upload_photo.php">Upload Photo</a> ]</p>
<?php    
  $photo = $_FILES["imgfile"]["name"]; 
  $username = mysql_escape_string($_POST['username']); 
  $max_image_size = '300000'; // Bytes only... 
  
  $random_digit = rand(000000,999999);  
  $renamed_photo = $random_digit.$_FILES["imgfile"]["name"];
  
  if (empty($photo)) {
     echo "<p>Sorry <i>$username</i> You Never Selected A Photo To Upload <a href=\"javascript: history.go(-1)\">Go Back</a> And Upload One!</p>";
     exit;
  }
  
  // Validation for Image.../////////////////////////////////////////////////////////////
  if ($_FILES['imgfile']['size'] > $max_image_size) {
   die ("<p>Sorry <i>$username</i> That Image Is Bigger Than The Allowed Size Of 3mb Please <a href=\"javascript: history.go(-1)\">Go Back</a> And Upload Another!</p>");
  }
  
  $uploadpath = "uploads/"; // <- Upload folder... 
  $uploadpath = $uploadpath.$renamed_photo; 
  if (!move_uploaded_file($_FILES["imgfile"]["tmp_name"], $uploadpath)) 
     die("<p>Sorry <i>$username</i> There Was An Error Uploading Your Image!"); 
     echo("<p><br />The Image (<b><font color=\"red\">" .$_FILES["imgfile"]["name"]. "</b></font>) Has Been Uploaded Successfully!<br />");
     
  // Create our query.../////////////////////////////////////////////////////////////////
  $sql = "UPDATE membership SET photo='$renamed_photo' WHERE username='$username'"; 

  // Run our query...////////////////////////////////////////////////////////////////////
  $result = mysql_query($sql, $conn) or die(mysql_error()); 

  // Show success message...///////////////////////////////////////////////////////////// 
  if($result) { 
  echo "<p><a href=\"javascript: history.go(-1)\">Back To My Account</a></p>"; 
}
?>

 

cheers

 

Graham

Link to comment
Share on other sites

You need to have code that will check if the user has previously uploaded an image before. Something like:

 

<?php
     // For register_global on PHP settings//////////////////////////////////////////////
     $member = $_COOKIE['member'];

     session_start(); // you must put this to read session variables/////////////////////

     if (empty($member) || !isset($member)) // fail to read the browser cookie///////////
     {
     // Try to read session
     if (empty($_SESSION['member']) || !isset($_SESSION['member']))
     {
           header("Location: login.php"); // redirect user to login//////////////////////
           exit;
     } else {
          $member = $_SESSION['member'];
  }
}
     //Includes... //////////////////////////////////////////////////////////////////////
     include("includes/db_connection.php");
     include("includes/constants.php");
     include("includes/header.php");
     include("includes/loginnav.php");
?>
<p align="left">Welcome, <b><font color="red"><? echo $_COOKIE['member'] ?></font></b>! (<a href="logout.php">Logout</a>) - [ <a href="change_password.php">Change Password Or E-mail Address</a> ]-[ <a href="edit_profile.php">Edit Profile</a> ]-[ <a href="upload_photo.php">Upload Photo</a> ]</p>
<?php

// Set the photo name from the form submission. If no image was submitted, this will be empty
$photo = $_FILES["imgfile"]["name"];
$random_digit = rand(000000,999999);
$renamed_photo = $random_digit.$_FILES["imgfile"]["name"];

$username = mysql_escape_string($_POST['username']); 

// Create a query to check if the current user has uploaded a photo previously, so we can overwrite it
$sql = "SELECT photo FROM membership WHERE username='$username' LIMIT 1";

// Set a varialbe for the DB connection
$result = mysql_query($sql, $conn) or die(mysql_error());

// Run the query in a while loop
while($row = mysql_fetch_array($result){
	// Set the photo name to the name already saved in the DB so we can overwrite the existing photo variables and file
	$photo = $row['photo'];
	$renamed_photo = $photo
}

  $max_image_size = '300000'; // Bytes only...
  
  if (empty($photo)) {
     echo "<p>Sorry <i>$username</i> You Never Selected A Photo To Upload <a href=\"javascript: history.go(-1)\">Go Back</a> And Upload One!</p>";
     exit;
  }
  
  // Validation for Image.../////////////////////////////////////////////////////////////
  if ($_FILES['imgfile']['size'] > $max_image_size) {
   die ("<p>Sorry <i>$username</i> That Image Is Bigger Than The Allowed Size Of 3mb Please <a href=\"javascript: history.go(-1)\">Go Back</a> And Upload Another!</p>");
  }
  
  $uploadpath = "uploads/"; // <- Upload folder... 
  $uploadpath = $uploadpath.$renamed_photo; 
  if (!move_uploaded_file($_FILES["imgfile"]["tmp_name"], $uploadpath)) 
     die("<p>Sorry <i>$username</i> There Was An Error Uploading Your Image!"); 
     echo("<p><br />The Image (<b><font color=\"red\">" .$_FILES["imgfile"]["name"]. "</b></font>) Has Been Uploaded Successfully!<br />");
     
  // Create our query.../////////////////////////////////////////////////////////////////
  $sql = "UPDATE membership SET photo='$renamed_photo' WHERE username='$username'"; 

  // Run our query...////////////////////////////////////////////////////////////////////
  $result = mysql_query($sql, $conn) or die(mysql_error()); 

  // Show success message...///////////////////////////////////////////////////////////// 
  if($result) { 
  echo "<p><a href=\"javascript: history.go(-1)\">Back To My Account</a></p>"; 
}
?>

Link to comment
Share on other sites

Hi Mate,

 

Thanks a lot for the code mate, it worked perfectly:) the only thing is when a user joins , they need to go into there CP and upload a photo from there, (the default value in mysql is blank obviously as nothin has been uploaded yet lol) so when a photo is uploaded it checks for a previous photo and finds nothing so rejects the new upoad is there a way i can remedy this?

 

thanks for the help mate

 

Graham

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.