RE: [PHP] How do you guys do this?
| From: | Michal Migurski | Date: | Sat, 31 Jan 2004 01:31:12 +0000 |
| Subject: | RE: [PHP] How do you guys do this? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-176426@lists.php.net to get a copy of this message | ||
>> I sure hope that there aren't 2 people uploading breadbox.jpg at the
>> same second. That'd be too weird! Now maybe if it was a porn site I
>> could see the same name, tina.jpg, michele.jpg, but again, the same
>> file name on the same second, what are the odds? <--anyone? :)
>
>You're absolutely right... when referring to timestamp_filename.jpg. I
>meant to (and thought I had) refer to using ONLY timestamp.jpg.
>
>If you only did timestamp.jpg it's very possible two people could upload
>ANY picture at the same time. Thus getting the same filename.
My usual lazy approach has been to use timestamp/filename -- images are
served up with the original filename, and are divided up into directories
to eliminate name conflicts. This is fine for a small-load site, but in
any case where you think you might have two users uploading two
identically named files at the same time (not all /that/ unlikely), and
you are using a database table to track assets/etc., you can use the
insertion id provided to you by your database.
table_name/primary_id/filename gets you a unique file every time, and
the file name is not munged.
There is also the helpful unix utility mktemp, which doesn't necessarily
need to make temporary files, it just guarantees a unique name. See
tempnam() in php for a good analogue.
---------------------------------------------------------------------
michal migurski- contact info and pgp key:
sf/ca http://mike.teczno.com/contact.html