I'm starting a small picture uploading thing and I have an ok uploaded, but if i upload two different pictures with the same name it overwrites the picture. I would like to know how i can have it change the file name to a different one. It might just be easier if it changes every file name upon upload, but I'm not sure how to do that. Is someone could tell me that would be great. Thanks!
If it helps here is the upload page http://img.adrianstech.com/upload.php
Picture Upload. Rename Same named file. Help Needed
Moderator: General Moderators
Re: Picture Upload. Rename Same named file. Help Needed
Here is a script that i made for a game site i play at.. users upload the replay and it converts it into username time and date into a replay folder.. I'm not going to modify it but I'll let you do some work with it..
upload script
script
upload script
Code: Select all
<input name="replay" type="file" id="replay" size="50">Code: Select all
$user_name = $_POST['user_name'];
$date = $_POST['date'];
$time = $_POST['time'];
$timer = explode(":", $time);
$time2 = $timer[0]."-".$timer[1]."-".$timer['2'];
$file_name = $_FILES['replay']['name'];
$file_name2 = $user_name."_".$date."_";
$file_name3 = $time2."_";
$new_file_name = $file_name2.$file_name3.$file_name;
$path= "replays/$user_name/".$new_file_name;
if($replay !=none)
{
if(copy($_FILES['replay']['tmp_name'], $path))
{
echo "<font class=\"txt\">Successful<BR/></font>";
}
/*else
{
echo "Error";
}*/
}
Re: Picture Upload. Rename Same named file. Help Needed
Use this:
iceman, are you aware that everything in POST is entirely up to the user? That script is insecure.
Code: Select all
$name = $_FILES['userfile']['name'];
while(file_exists('files/'.$name)) {
$name = mt_rand().$_FILES['userfile']['name'];
}iceman, are you aware that everything in POST is entirely up to the user? That script is insecure.
Re: Picture Upload. Rename Same named file. Help Needed
To really make sure you won't over write any existing file with the same name, use the Unix time stamp with a 1 second sleep between uploads. Below is a generic logic:
Code: Select all
for(all_files_to_be_uploaded){
$new_file = time().$file; //or you could extract the file extension here and just do time().$fil_extension
//do the file upload here
sleep(1);
}
Re: Picture Upload. Rename Same named file. Help Needed
First of i didn't say anything about my script being secure i was just showing him how i done mine. They come to this forum for help not to give them things at there asking.. you help them in the right direction to show them the way of programing.. you think all the programmers you talk to today got where they are because someone gave them scripts? no they learned by reading and getting help in the right direction.Cut wrote:Use this:
Code: Select all
$name = $_FILES['userfile']['name']; while(file_exists('files/'.$name)) { $name = mt_rand().$_FILES['userfile']['name']; }
iceman, are you aware that everything in POST is entirely up to the user? That script is insecure.
- The_Anomaly
- Forum Contributor
- Posts: 196
- Joined: Fri Aug 08, 2008 4:56 pm
- Location: Tirana, Albania
Re: Picture Upload. Rename Same named file. Help Needed
"Helping someone in the right direction to show them the way of programming," through insecure code is about as much of a help as shooting them in the head. If you're demonstrating a certain technique without regard for security, then say that in your post. Or at least, if an error is pointed out, accept it and learn from it. Honestly, I've learned so much from all the responses to MY responses.iceman83 wrote:First of i didn't say anything about my script being secure i was just showing him how i done mine. They come to this forum for help not to give them things at there asking.. you help them in the right direction to show them the way of programing.. you think all the programmers you talk to today got where they are because someone gave them scripts? no they learned by reading and getting help in the right direction.Cut wrote:Use this:
Code: Select all
$name = $_FILES['userfile']['name']; while(file_exists('files/'.$name)) { $name = mt_rand().$_FILES['userfile']['name']; }
iceman, are you aware that everything in POST is entirely up to the user? That script is insecure.
Instead of getting all defensive, and you see your error, thank the person who corrected you, and learn from it.