tags:

views:

212

answers:

5

Well, I need to save images with unique filenames and store the file names in the database. I used to do MD5 hash and save the image with the filename of value obtained by hash.

However I would like to cut down the unnecessary space usage from 32 characters to 10-12 characters.

I dont want to substr() the obtained md5 hash to 12 characters.

Rather than that is there a way to create a custom hash of 10-12 characters ?

+1  A: 

Maybe using CRC32 instead of MD5? It is 8 characters, not 32, but it is nearer to your goal.

Cedric
yeah so its 26 ^ 8 in all .. i guess its enough
atif089
but I dont want them to collide thats the reason I am looking for 10-12 characters
atif089
+1  A: 

PHP's Hash message digest framework provides tons of hash algorithms. Have fun!

To determine each hash's length, see Wikipedia.

Boldewyn
great! thanks for this :)
atif089
You're welcome!
Boldewyn
+1  A: 

CRC32 is 8 ASCII characters long.

poke
+1  A: 

You can represent the 128 Bit MD5 hash values with just 16 characters (8 bit per character instead of just 4).

Gumbo
can you please give me an example ? thanks
atif089
Example: 1 byte = 1 character. 128 bit = 16 byte. => 128 bit = 16 characters. `md5("string", true)` delivers 16 characters. **Beware!** The string will also contain \0 and such.
Boldewyn
Would it be safe to insert the same value in a mysql database (utf-8 collation) or do i need to perform an filters?
atif089
@atif089: Better use BINARY ASCII as the characters ≥ 128 use more than one byte in UTF-8. See also http://stackoverflow.com/questions/504268
Gumbo
echo md5('1_DSC00144_' . time(), true); returns me "ƒ|^×íñu¬‹_ë õòþ", I need to use an ASCII string to save in the database so that I can use it in the URL. bin2hex is making it back to 32 characters once again.
atif089
Yes, that's the deal, when you only use the upper 64bit of ASCII ;-)
Boldewyn
LoL, using **crc32b** as of now. But there should be something more appropriate as well like most of the image hosting website, facebook, and orkut use.. Will do some research :)
atif089
@atif089: You could then use 6 Bits instead and use the characters Base64 does.
Gumbo
Quote from OP's comment: "I need to use an ASCII string to save in the database so that I can use it in the URL". Why don't you just set DB table to UTF-8 and use URL-encoding? After all it sounds like that you aren't really aware about the relationship between bytes/bits and characters and the encoding story. I'd suggest to do a bit more research on that area.
BalusC
@BalusC: UTF-8 would use more than just one byte for the characters ≥ 128. Plain binary ASCII is better.
Gumbo
+2  A: 

What about using the following function Tempnam

http://php.net/manual/en/function.tempnam.php

"Creates a file with a unique filename, with access permission set to 0600, in the specified directory. If the directory does not exist, tempnam() may generate a file in the system's temporary directory, and return the name of that. "

Lizard
I don't actually want to create a file, I want to move an uploaded file with a unique name to a directory. Taking this function into consideration, I will have to create a tmp file, read uploaded file, overwrite tmp file. Wouldn't you think it will be slower complared to just moving a file from one location to another ?
atif089
Depending on the size of the files and the regularity of the uploads, the extra overhead should be minimal
Lizard