How do i stop overwriting of uploaded files?

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
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

How do i stop overwriting of uploaded files?

Post by mikegotnaild »

If my users upload a file with the same name as one that is already in the folder what type of coding do i have to do to stop this... I WANT to keep the file names in-tact though. Maybe i could have a folder automatically created for them and then put the files into that folder...?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: How do i stop overwriting of uploaded files?

Post by Roja »

mikegotnaild wrote:If my users upload a file with the same name as one that is already in the folder what type of coding do i have to do to stop this... I WANT to keep the file names in-tact though. Maybe i could have a folder automatically created for them and then put the files into that folder...?
You could do something like:

APP_NAME-HHMM-FILENAME.EXT

So if your application was "webthing", then it could prepend the filename to include webthing, and the time it was saved. That way, very very few collisions.
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

could i get a code example please?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Roja is refering to the date function, e.g.:

Code: Select all

$filename="myFile_".date("His");
Alternatively, you could check whether the file exists already with
[php_man]file_exists[/php_man]. If true, then prompt user.
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

Ok i did that but when i echo $filename it shows the date information. I need it to show the file name.
Post Reply