0

So I've followed this link http://www.w3schools.com/php/php_file_upload.asp to make a photo uploading capability but rather than it saying that the file has already been uploaded when there is another file of the same name, how would i just make it change the file name to something else that hasn't been used before.

in other words, if there is another photo of the same name I still want it to upload but I don't want it to overwrite the existing one.

I basicaly want to replace this with something else to do that

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;

so that its something like this

// Check if file already exists
if (file_exists($target_file)) {
"do something to change file name"
$uploadOk = 1;

UPDATE: I got it to work. I just made it so that it renames every image to a unique id like stated bellow. thanks

also in that suggestion it should be this

$newTarget_file = $target_dir . uniqid() . "." . end($target_file);

1 Answers1

1

add random numers to rename file

$name = explode(".",$_FILES["file"]["name"]);
$newName = rand(1,99999) . '.' .end($name);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newName );

Or you also put if condition to verify if file name is exist or not. And if file is available add something to rename your file.

Parag Soni
  • 713
  • 7
  • 14