note 35254 deleted from function.md5 by mazzanet
| From: | mazzanet@php.net | Date: | Mon, 03 Oct 2005 09:25:17 +0000 |
| Subject: | note 35254 deleted from function.md5 by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96253@lists.php.net to get a copy of this message | ||
Note Submitter: simms
----
the md5( ) function can also be used to elegantly generate unique file names for files uploaded to a
site.
in case md5( ) returns the same result twice (a one in 3.4028236692093846346337460743177e+38
probability), you can wrap it in a while loop.
the following example is illustrative -- in my case i like to keep the original file extension (up
to 4 characters, which preserves extensions like "jpeg" and "mpeg"), and i use
the date function to add an extra element of randomness to the final result:
<?
do
{
$finalName = md5( $originalName . date( "U" ) ) . substr( $originalName, strlen(
$originalName ) - 5, 5 );
}
while( file_exists( "/upload_directory/" . $finalName ) );
?>
..this gives you final names like
df2921c9f5b31f67dda07443f1c8f99bp.png