Jump to content

Upload file - change name if file has same name


jjmusicpro

Recommended Posts

Right now i have an uploading script working, a basic one.

However, if someone uploads a file with the same name, it will overwirte the previous one.

 

How can i make it so it checks to see if there is a file with the same name, if so, just put a number on the end that increments or something.?

 

Thanks for your help

 

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded.<br>";
echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\">";
} else{
    echo "There was an error uploading the file, please try again!";
}

i dont know how to reloop it back through, and keep checking if that file name already exsists.

so if there is already a test.gif, it would go to test.gif1, however, if there is already a test.gif1..i need it to go to the next number.

or cant i add some weird time hash on the end of the file name?

like this... (i have not tried executing this code.. )

 

<?php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

$i = 0;
while(file_exists($target_path).$i) {
$i++;
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$i)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded.<br>";
echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\">";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

so this is what i have but its not changing name

 



<?
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

$i = 0;
while(file_exists($target_path.$i)) {
$i++;
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$i)) {
echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\"><br><br>";
echo "<b>Direct Link</b><br>";
echo "<textarea cols=\"50\" rows=\"3\">http://www.blintool.com/uploads/".basename( $_FILES['uploadedfile']['name'])."</textarea>";
echo "<br><br>";
echo "<b>Myspace / FaceBook / Hi5 / Orkut Links</b><br>";
echo "<textarea cols=\"50\" rows=\"3\"><a href=\"http://www.blingtool.com\"><img src=\"http://www.blingtool.com/uploads/".basename( $_FILES['uploadedfile']['name'])."></a></textarea>";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

I looked in my file directory and what its doing is, adding the number on the end of the file name, so if i uploaded me.gif already, its making a me.gif1 not me1.gif.

 

Do you think it would be better to just add a 10 digit random number to the end?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.