0

I am building a forum with php and MySQL and I want to append current time to each image that users upload for their profile. I used time() function on each image file uploaded and it worked for the image file but I have trouble inserting the new filename into the database. I wanted to give each image a unique name to prevent override.

OK here is what I did: I stored the current time as $time and the filename in a variable, $photo and I tried to insert that variable’s value using $photo .= $time into the database so it has the filename as I did with each uploaded image. However the original filename is inserted into the database in every attempt. Any workarounds?

$image = $_FILES['photo']['name'];
$time = time();
$image .= $time;

delete the existing photo

delete(image_dir/$row['current_photo.jpg']); 

//does not work, but i want something like that

if(move_uploaded_file($_FILES['photo']['tmp_name'], 'image_dir/$image') {
   $query = "INSERT INTO profile (user_id, profile_photo) VALUES($id, $image)";
   mysqli_query($dbc, $query);
   if(mysqli_affected_rows($dbc) == 1) { 
       echo "added to the database!"; 
   }else {
      echo "failed to add photo to the database"; 
   }
}else {
   echo "failed to upload photo"; 
} 

how can i give the uploaded image unique the name since the original image name gets inserted in the database in every try i make?

i know the code looks funny :). just want to show the logic.

1
  • 2
    Could you show your code or your sql? Commented Jan 20, 2014 at 12:02

2 Answers 2

0

If you need a unique id, you can use the uniqid function

$name=uniqid();
Sign up to request clarification or add additional context in comments.

Comments

0

you may need to use

$filename=uniqid();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.