Created
November 29, 2011 23:57
-
-
Save SteelPangolin/1407245 to your computer and use it in GitHub Desktop.
fake mkdtemp() for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create a directory in the system temp directory with a hard-to-predict name. | |
* Does not have the guarantees of the actual BSD libc function or Python tempfile function. | |
* @param string $suffix Append to the new directory's name | |
* @param string $prefix Prepend to the new directory's name | |
* @return string The path of the new directory. | |
*/ | |
function mkdtemp($suffix = '', $prefix = 'tmp') | |
{ | |
$id = strtr(base64_encode(openssl_random_pseudo_bytes(6)), '+/', '-_'); | |
$tempdir = sys_get_temp_dir(); | |
$path = "{$tempdir}/{$prefix}{$id}{$suffix}"; | |
mkdir($path, 0700, TRUE); | |
return $path; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment