» tagged pages
» logout
PHP
Return to PHP Code Snippets

Generate unique filenames in PHP

Tags Applied to this Entry

1 person has tagged this page:
This code is from http://www.weberdev.com/get_example-3543.html.

It produces filenames similar to this: 4293d8fd-ab63-7c82.tmp


function uniqueFilename($strExt = '.tmp') {
// explode the IP of the remote client into four parts
$arrIp = explode('.', $_SERVER['REMOTE_ADDR']);
// get both seconds and microseconds parts of the time
list($usec, $sec) = explode(' ', microtime());
// fudge the time we just got to create two 16 bit words
$usec = (integer) ($usec * 65536);
$sec = ((integer) $sec) & 0xFFFF;
// fun bit--convert the remote client's IP into a 32 bit
// hex number then tag on the time.
// Result of this operation looks like this xxxxxxxx-xxxx-xxxx
$strUid = sprintf("%08x-%04x-%04x", ($arrIp[0] // tack on the extension and return the filename
return $strUid . $strExt;
}
Username:
Password:
(or Cancel)