Picture Upload. Rename Same named file. Help Needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wee493
Forum Newbie
Posts: 22
Joined: Sat Sep 06, 2008 3:05 pm

Picture Upload. Rename Same named file. Help Needed

Post by wee493 »

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
iceman83
Forum Newbie
Posts: 9
Joined: Sat Sep 06, 2008 1:34 pm

Re: Picture Upload. Rename Same named file. Help Needed

Post by iceman83 »

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

Code: Select all

<input name="replay" type="file" id="replay" size="50">
script

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";
}*/
}
 
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: Picture Upload. Rename Same named file. Help Needed

Post by Cut »

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.
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: Picture Upload. Rename Same named file. Help Needed

Post by tamamk »

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);
}
 
iceman83
Forum Newbie
Posts: 9
Joined: Sat Sep 06, 2008 1:34 pm

Re: Picture Upload. Rename Same named file. Help Needed

Post by iceman83 »

Cut wrote:Use this:

Code: Select all

$name = $_FILES['userfile']['name'];
while(file_exists('files/'.$name)) {
&nbsp;$name = mt_rand().$_FILES['userfile']['name'];
}

iceman, are you aware that everything in POST is entirely up to the user? That script is insecure.
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.
User avatar
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

Post by The_Anomaly »

iceman83 wrote:
Cut wrote:Use this:

Code: Select all

$name = $_FILES['userfile']['name'];
while(file_exists('files/'.$name)) {
&nbsp;$name = mt_rand().$_FILES['userfile']['name'];
}

iceman, are you aware that everything in POST is entirely up to the user? That script is insecure.
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.
"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.

Instead of getting all defensive, and you see your error, thank the person who corrected you, and learn from it.
Post Reply