示例#1
0
文件: Temp.php 项目: sndsgd/fs
 /**
  * Create a temp file
  *
  * @param string $prefix A prefix for the filename
  * @return \sndsgd\fs\File
  */
 public static function createFile(string $prefix, int $maxAttempts = 10) : entity\FileEntity
 {
     $tmpdir = static::getDir();
     $prefix = \sndsgd\Fs::sanitizeName($prefix);
     $attempts = 1;
     do {
         if ($attempts > $maxAttempts) {
             throw new \RuntimeException("failed to create temp file; " . "reached max number ({$maxAttempts}) of attempts");
         }
         $rand = \sndsgd\Str::random(10);
         $path = "{$tmpdir}/{$prefix}-{$rand}";
         $attempts++;
     } while (file_exists($path));
     touch($path);
     $file = new entity\FileEntity($path);
     static::registerEntity($file);
     return $file;
 }