Forum Moderators: coopster

Message Too Old, No Replies

uploading 2 files to a folder in PHP

I want to upload 2 files to a folder name "images"..

         

Knowledge seeker

9:42 am on Nov 19, 2009 (gmt 0)

10+ Year Member



I want to upload 2 files to a folder name "images". I did this easley and it uploaded in the "images" folder. but when I upload the another files again with the same file names that alread exist, it replace the existing file or delete it.

So what I want is that, my every file should have a unique name so that it cannot be overwrite or replace by the another file upload to this foler. All my files uses jpg extensions.

Waiting for your kind response.

Thanks in advance.

Best Regards..

jman11

2:09 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



explode the filename with extension and name, then add a rand() function to the end of the filename so it has a random string of numbers at the end of the name.

StoutFiles

4:45 pm on Nov 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another way to do it would have a .txt file that keeps a count. You could pull out the number in the .txt file, add the number to the file name, increment the number, then place the number back in the .txt file.

That way you can easily keep track of the order of when the files were uploaded.

rocknbil

8:29 pm on Nov 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would use built in date and time functions to create the file name. If you use rand() the potential exists that the same file name/number combination will come up again.

Unfortunately, but an advantage in this case, time always marches forward.

A second option is if it ever gets large, you will need some scheme to make sense of it. Address it now before it does. In a member based scenario,

/images/members/12345

where 12345 represents the member's id. Then you can just extract the uploaded file name and use what they have uploaded.

In this case, simple do a check for file exists on upload. Foreseeing potential problems, you can have a checkbox on the form: "Overwrite this file if it already exists." If unchecked and exists, return to upload form with the error, if not checked, just let it overwrite.

hal12b

3:32 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



I would use random and time. Not sure how much traffic your sites gets, but it is likely somebody could do something at the same exact time.

jman11

7:38 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



i would do :


$time = date(blah);
$rand = rand();
$filename = $name.'-'.hash($time.$rand);

rocknbil

7:56 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would use random and time.

In seconds, and have the exact same file name?

I 'spose it's a possibility. Probability, only in Hitchhiker's Guide. :-)