38 parent::__construct( $path );
40 if ( self::$pathsCollect ===
null ) {
41 self::$pathsCollect = [];
42 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
55 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
56 $ext = ( $extension !=
'' ) ?
".{$extension}" :
'';
59 while ( $attempts-- ) {
60 $hex = sprintf(
'%06x%06x', mt_rand( 0, 0xffffff ), mt_rand( 0, 0xffffff ) );
61 if ( !is_string( $tmpDirectory ) ) {
65 Wikimedia\suppressWarnings();
66 $newFileHandle = fopen( $path,
'x' );
67 Wikimedia\restoreWarnings();
68 if ( $newFileHandle ) {
69 fclose( $newFileHandle );
70 $tmpFile =
new self(
$path );
71 $tmpFile->autocollect();
86 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
87 $tmpDir[] = sys_get_temp_dir();
88 $tmpDir[] = ini_get(
'upload_tmp_dir' );
89 foreach ( $tmpDir as $tmp ) {
90 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
100 if ( strtoupper( substr( PHP_OS, 0, 3 ) ) ===
'WIN' ) {
101 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
102 if ( !file_exists( $tmp ) ) {
105 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
110 throw new RuntimeException(
111 'No writable temporary directory could be found. ' .
112 'Please explicitly specify a writable directory in configuration.' );
121 $this->canDelete =
false;
122 Wikimedia\suppressWarnings();
123 $ok = unlink( $this->path );
124 Wikimedia\restoreWarnings();
126 unset( self::$pathsCollect[$this->path] );
137 public function bind( $object ) {
138 if ( is_object( $object ) ) {
139 if ( !isset( $object->tempFSFileReferences ) ) {
141 $object->tempFSFileReferences = [];
143 $object->tempFSFileReferences[] = $this;
155 $this->canDelete =
false;
157 unset( self::$pathsCollect[$this->path] );
168 $this->canDelete =
true;
181 foreach ( self::$pathsCollect as $path ) {
182 Wikimedia\suppressWarnings();
184 Wikimedia\restoreWarnings();
192 if ( $this->canDelete ) {
wfTempDir()
Tries to get the system directory for temporary files.
Class representing a non-directory file on the file system.
string $path
Path to file.
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
__construct( $path)
Sets up the file object.
bind( $object)
Clean up the temporary file only after an object goes out of scope.
autocollect()
Set flag clean up after the temporary file.
__destruct()
Cleans up after the temporary file by deleting it.
purge()
Purge this file off the file system.
preserve()
Set flag to not clean up after the temporary file.
bool $canDelete
Garbage collect the temp file.
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
static purgeAllOnShutdown()
Try to make sure that all files are purged on error.
static getUsableTempDirectory()
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
if(!is_readable( $file)) $ext