note 35254 added to function.md5

From: Date: Mon, 25 Aug 2003 18:54:22 +0000
Subject: note 35254 added to function.md5
Groups: php.notes 
Request: Send a blank email to php-notes+get-55054@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.md5.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35254 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35254&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35254&report=yes

« previous php.notes (#55054) next »