By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,024 Members | 2,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,024 IT Pros & Developers. It's quick & easy.

How to replace image form folder by another (NEW) image uploaded by user?

ilya Kraft
100+
P: 134
Hello,

Alright, so I came across the following problem. I have system where users can upload their profile images. Images get stored in folders with member id's. The issue is: When user uploads image it get's set as their profile image, but when they upload new image it get's stored in the folder with old image, so now they have 2 profile images. And this causes trouble. I need to automatically delete old image when user uploads new image, so replace old to new.
I will provide scripts that I use for upload now, So if you have any ideas on how to fix this problem I would really appreciate it, Thank You )))

Upload Form Script
Expand|Select|Wrap|Line Numbers
  1. <form enctype="multipart/form-data" method="post" action="image_upload_script.php">
  2.     Choose your file here:
  3.     <input name="uploaded_file" type="file"/><br /><br />
  4.     <input type="submit" value="Upload It"/>
  5.     </form>
  6.  


image_upload_script.php (PHP parsing script to upload image to user folders)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (!$_SESSION['idx']) { 
  3.     $msgToUser = '<br /><br /><font color="#FF0000">Only site members can do that</font><p><a href="register.php">Join Here</a></p>';
  4.     include_once 'msgToUser.php'; 
  5.     exit(); 
  6. } else if ($logOptions_id != $_SESSION['id']) {
  7.     $msgToUser = '<br /><br /><font color="#FF0000">Only site members can do that</font><p><a href="register.php">Join Here</a></p>';
  8.     include_once 'msgToUser.php'; 
  9.     exit(); 
  10. }
  11. $id = $logOptions_id;
  12. // Access the $_FILES global variable for this specific file being uploaded
  13. // and create local PHP variables from the $_FILES array of information
  14. $fileName = $_FILES["uploaded_file"]["name"]; // The file name
  15. $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
  16. $fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
  17. $fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
  18. $fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
  19. $kaboom = explode(".", $fileName); // Split file name into an array using the dot
  20. $fileExt = end($kaboom); // Now target the last array element to get the file extension
  21. // START PHP Image Upload Error Handling --------------------------------------------------
  22. if (!$fileTmpLoc) { // if file not chosen
  23.     echo "ERROR: Please browse for a file before clicking the upload button.";
  24.     exit();
  25. } else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
  26.     echo "ERROR: Your file was larger than 5 Megabytes in size.";
  27.     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  28.     exit();
  29. } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
  30.      // This condition is only if you wish to allow uploading of specific file types    
  31.      echo "ERROR: Your image was not .gif, .jpg, or .png.";
  32.      unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  33.      exit();
  34. } else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
  35.     echo "ERROR: An error occured while processing the file. Try again.";
  36.     exit();
  37. }
  38. // END PHP Image Upload Error Handling ----------------------------------------------------
  39. // Place it into your "uploads" folder mow using the move_uploaded_file() function
  40. $moveResult = move_uploaded_file($fileTmpLoc, "members/$id/$fileName");
  41. // Check to make sure the move result is true before continuing
  42. if ($moveResult != true) {
  43.     echo "ERROR: File not uploaded. Try again.";
  44.     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  45.     exit();
  46. }
  47. unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  48. ?>
  49.  
Jun 19 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.