Example #1
0
 /**
  * Make a new temporary file on the file system.
  * Temporary files may be purged when the file object falls out of scope.
  *
  * @param string $prefix
  * @param string $extension
  * @return TempFSFile|null
  */
 public static function factory($prefix, $extension = '')
 {
     wfProfileIn(__METHOD__);
     $base = wfTempDir() . '/' . $prefix . wfRandomString(12);
     $ext = $extension != '' ? ".{$extension}" : "";
     for ($attempt = 1; true; $attempt++) {
         $path = "{$base}-{$attempt}{$ext}";
         wfSuppressWarnings();
         $newFileHandle = fopen($path, 'x');
         wfRestoreWarnings();
         if ($newFileHandle) {
             fclose($newFileHandle);
             break;
             // got it
         }
         if ($attempt >= 5) {
             wfProfileOut(__METHOD__);
             return null;
             // give up
         }
     }
     $tmpFile = new self($path);
     $tmpFile->autocollect();
     // safely instantiated
     wfProfileOut(__METHOD__);
     return $tmpFile;
 }
Example #2
0
 /**
  * Make a new temporary file on the file system.
  * Temporary files may be purged when the file object falls out of scope.
  *
  * @param string $prefix
  * @param string $extension
  * @return TempFSFile|null
  */
 public static function factory($prefix, $extension = '')
 {
     $ext = $extension != '' ? ".{$extension}" : '';
     $attempts = 5;
     while ($attempts--) {
         $path = wfTempDir() . '/' . $prefix . wfRandomString(12) . $ext;
         MediaWiki\suppressWarnings();
         $newFileHandle = fopen($path, 'x');
         MediaWiki\restoreWarnings();
         if ($newFileHandle) {
             fclose($newFileHandle);
             $tmpFile = new self($path);
             $tmpFile->autocollect();
             // Safely instantiated, end loop.
             return $tmpFile;
         }
     }
     // Give up
     return null;
 }
Example #3
0
 /**
  * Make a new temporary file on the file system.
  * Temporary files may be purged when the file object falls out of scope.
  *
  * @param string $prefix
  * @param string $extension Optional file extension
  * @param string|null $tmpDirectory Optional parent directory
  * @return TempFSFile|null
  */
 public static function factory($prefix, $extension = '', $tmpDirectory = null)
 {
     $ext = $extension != '' ? ".{$extension}" : '';
     $attempts = 5;
     while ($attempts--) {
         $hex = sprintf('%06x%06x', mt_rand(0, 0xffffff), mt_rand(0, 0xffffff));
         if (!is_string($tmpDirectory)) {
             $tmpDirectory = self::getUsableTempDirectory();
         }
         $path = wfTempDir() . '/' . $prefix . $hex . $ext;
         MediaWiki\suppressWarnings();
         $newFileHandle = fopen($path, 'x');
         MediaWiki\restoreWarnings();
         if ($newFileHandle) {
             fclose($newFileHandle);
             $tmpFile = new self($path);
             $tmpFile->autocollect();
             // Safely instantiated, end loop.
             return $tmpFile;
         }
     }
     // Give up
     return null;
 }