Forum Moderators: coopster

Message Too Old, No Replies

Unique filenames from same string?

How to create unique filenames from the same string

         

NooK

6:53 am on Aug 30, 2007 (gmt 0)

10+ Year Member



I have a function which basically creates a file with the name based on a string (Ex: "Thsi is a test" becomes "This-is-a-test.php").

My problem is that the strings can be the same sometimes but I would like to create one file for each (Thus with a unique filename even if the string is the same)

I thought of appending the md5 of the time and date the function was run to the filename but it seems md5 creates a 13 char string which is a bit too long for a filename.

Is there anyway to tackle this problem so that I get something shorter (Ex: "This is a test" becomes "this-is-a-test-3827.php") while still having a unique name?

Best Regards

NooK

phparion

6:57 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



after creating md5 13 chars long string use

$tail = substr($md5String,0,5); // fetch first five characters out of thirteen

then attach it with file name with . operator

$finaFileName = $filename."-".$tail.".php";

SUBSTR manual [php.net]

Habtom

7:00 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why MD5 of the time? Why not just the timestamp, or id of the row where the data is coming from.

If you are pulling it out from a DB, the rows could be similar to the following:

. . .
12 ¦ This is a string ¦ dada
. . .
17 ¦ This is a string ¦ dada2
. . .

How about taking those ids? There must be something unique about them that enables you to identify them on the file names as well.

If not just append the timestamp

$string = $string."-".time();

Habtom

NooK

7:46 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Appending just the first 5 chars of the md5 hash would take the uniqueness of the string I think causing problems in the long run.

Appending the time steamp does sound like a good idea, had not thought of that.

Thanks I'll give it a try and see how it goes.

Best Regards

NooK