Skip to content

Instantly share code, notes, and snippets.

@SteelPangolin
Created November 29, 2011 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteelPangolin/1407245 to your computer and use it in GitHub Desktop.
Save SteelPangolin/1407245 to your computer and use it in GitHub Desktop.
fake mkdtemp() for PHP
<?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