MediaWiki master
TempFSFileFactory.php
Go to the documentation of this file.
1<?php
2
4
10 private $tmpDirectory;
11
16 public function __construct( $tmpDirectory = null ) {
17 $this->tmpDirectory = $tmpDirectory;
18 }
19
28 public function newTempFSFile( $prefix, $extension = '' ) {
29 $ext = ( $extension != '' ) ? ".{$extension}" : '';
30 $tmpDirectory = $this->tmpDirectory;
31 if ( !is_string( $tmpDirectory ) ) {
32 $tmpDirectory = TempFSFile::getUsableTempDirectory();
33 }
34
35 $attempts = 5;
36 while ( $attempts-- ) {
37 $hex = sprintf( '%06x%06x', mt_rand( 0, 0xffffff ), mt_rand( 0, 0xffffff ) );
38 $path = "$tmpDirectory/$prefix$hex$ext";
39 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
40 $newFileHandle = @fopen( $path, 'x' );
41 if ( $newFileHandle ) {
42 fclose( $newFileHandle );
43 $tmpFile = new TempFSFile( $path );
44 $tmpFile->autocollect();
45 // Safely instantiated, end loop.
46 return $tmpFile;
47 }
48 }
49
50 // Give up
51 return null; // @codeCoverageIgnore
52 }
53}
54
56class_alias( TempFSFileFactory::class, 'MediaWiki\FileBackend\FSFile\TempFSFileFactory' );
newTempFSFile( $prefix, $extension='')
Make a new temporary file on the file system.
This class is used to hold the location and do limited manipulation of files stored temporarily (this...