开发者

How to generate short filenames for uploaded photos?

While uploading new photos to a linux server with PHP, I'm having to give them unique names (using it's file name is ruled out). There's no interaction with a DB so getting unique IDs is also not possible.

I thought of using DATE+TIME.jpg, but that's just too long.

So what is the be开发者_Python百科st method to create the shortest possible unique names with PHP?


Use tempnam().

$filename = tempnam  ($dir, $prefix)

It creates a file with a unique name in the specified directory. The generated file name is pretty short and guaranteed to be unique.


The shortest safe way is to use uniqid() function.


You could also use the same sort of hashing that url shorteners like bit.ly use to generate their unique 5 character folder names (e.g bit.ly/x3hs0).

There's an implementation in PHP here.


Create an index file; this file contains the last ID that was used for a file. When a new file needs to be created:

  1. lock the index file (if your OS supports it)
  2. Read the ID
  3. Add 1 to the ID
  4. Save the file
  5. Unlock the file
  6. Create a file with that ID

regards,
Stijn


Well, the problem is that you want something unique AND short. You can't really have both.

The densest representation of a random number that is human-readable would be something like base64 (substitute valid characters for any characters that shouldn't be in a filename). However that will still mean there are "only" 64 variations for each character in the filename.

If you want a simple filename, just use an incrementing number and encode it in base64 (pad it out if you want a minimum length for the name). If you want it to be non-guessable, you need a random number generator, but then you need to make sure to avoid doubles or you'll end up overwriting the file. This chance for disaster is higher the shorter the names are.

You could probably use your own custom equivalent of base64 with more characters than just the 64 provided by that encoding, but then you'd have to check what your OS considers a valid filename and you'd still have to make sure the client can access it properly (if it isn't just a temporary data store). The easiest way to do this is to create a string with every character you want to be legit in it and then convert the number's base into whatever the length of the string is (there are explanations of base conversion on Google if you don't know how to do that). That would allow for even higher information density and just even shorter filenames.


What I always do is set the filename as an MD5 hash of the file itself - the side effect of this is it avoids duplicate uploads taking space, plus you can verify data integrity fairly easily.

If you do want it to be shorter you can start chopping bits off of the end, but the more you do this the more you risk a filename collision, but from experience the taking first eight or so characters generally guarantees uniqueness provided you dont upload too much.


If I were you I'd just generate a random number which is about 6 characters long, and do in in a do {} (while) loop till it's unique.


One way is just using random filenames. Like rand(0,99999999)+".jpg". The probability of a collision is very small (if you do not have LOTS of images), and you could easily check if by some freak chance the name is already taken and get a new one in that case.


Shortest is not the best way. I think you can use md5($date+$time) function to get the unique file name. It will be like this: 8d41627e46d5b8556d0d3e30ec15538e.

Also, If you think that there will be big count of uploaded files, you can divide your directory structure by folder with names, for example, consists of first 2 letters of md5() hash:

8d/
  + 8d41627e46d5b8556d0d3e30ec15538e.jpg
  + 8d897234ed899a523c88273c009c12c2.jpg
ce/
  + ce2389898ace097cc667e134abb61729.jpg
...


You can use a random number and the date+time schem and check if the name exists in a while() loop :

$new_name = md5( rand(0,99999) . date('Ymd') . microtime(true) ) . $extension;
while ( file_exists($path . $new_name) ) 
   $new_name = md5( rand(0,99999) . date('Ymd') . microtime(true) ) . $extension;

where $new_name is the short name of your uploaded file

$extension is the extension of you file (like .jpg)

$path is the full path of the directory where the file has to be saved

The use of rand() plus microtime(true) inscrease the chance to find a unique name for the first shot...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜