473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

ilya Kraft
134 100+
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
0 1475

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

Similar topics

4
by: Bruce W...1 | last post by:
My PHP html file includes a header include, a form, and a footer include. After the user submits their information I want to replace the form part of the page with a thank you note. All the...
32
by: Eli | last post by:
How can I POST a form into a new window where I control the size and other attributes of the new window? Also. Are there any implications, perhaps due to browser security (Interne Explorer?)...
2
by: Brian K. Williams | last post by:
I am looking for a way to read an Image from SQL directly to a new Image or Placeholder. I have not been able to find any examples other than Response.OutputStream.Write or similar. Thanks for...
3
by: gsb58 | last post by:
Hi! A mainform is being used to show records from a table in an sql database. A button on the main form will load a new form that allows the user to add, delete, update and search certain...
8
by: handersonVA | last post by:
Hello, I'd like to put small images around a big image, so when a small image is clicked, that image is shown as a big image in the center. how should I do that? I will use a table tag to organize...
2
by: neuneudr | last post by:
Hi all, I've been trying JavaScript a little bit. I beginned by changing an image using a very simple: document.getElementById(...).src="new image" Which worked fine. However this does...
1
by: menyki | last post by:
am still finding it difficult as a new java user. am designing a software that has multiple forms, controls such as combobox and so on and finding it difficult to do the following. i want some...
8
by: Jomba | last post by:
Hi everyone, In folder image i have 24 pictures, and i would like to have some javascript that will every hour put another picture as background image. I would like to do that on this page...
2
by: =?Utf-8?B?SmVmZlAtPg==?= | last post by:
I want to retrieve an image file (TIF) convert it to a bmp file and enable sizing. I know how to do the converting & sizing part, and store any atributes to file location in the webconfig, what...
5
by: RuiT | last post by:
Hello, I'm uploading images to a folder and I want to display each new image on a web page every time it is saved in the folder. I can display the initial image, which is already in the folder,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.