PHPDaemon\FS\FileSystem::genRndTempnam PHP Méthode

genRndTempnam() public static méthode

Returns random temporary file name
public static genRndTempnam ( string $dir = null, string $prefix = 'php' ) : string
$dir string Directory
$prefix string Prefix
Résultat string Path
    public static function genRndTempnam($dir = null, $prefix = 'php')
    {
        if (!$dir) {
            $dir = sys_get_temp_dir();
        }
        static $n = 0;
        return $dir . '/' . $prefix . str_shuffle(md5(str_shuffle(microtime(true) . chr(mt_rand(0, 0xff)) . Daemon::$process->getPid() . chr(mt_rand(0, 0xff)) . ++$n . mt_rand(0, mt_getrandmax()))));
    }

Usage Example

Exemple #1
0
 /**
  * Generates closure tempnam handler
  * @param  $dir
  * @param  $prefix
  * @param  $cb
  * @param  $tries
  */
 protected static function tempnamHandler($dir, $prefix, $cb, &$tries)
 {
     $cb = CallbackWrapper::forceWrap($cb);
     if (++$tries >= 3) {
         call_user_func($cb, false);
         return;
     }
     $path = FileSystem::genRndTempnam($dir, $prefix);
     FileSystem::open($path, 'x+!', function ($file) use($dir, $prefix, $cb, &$tries) {
         if (!$file) {
             static::tempnamHandler($dir, $prefix, $cb, $tries);
             return;
         }
         call_user_func($cb, $file);
     });
 }