Home > Article > Backend Development > The difference between creating temporary files tempnam and tmpfile in php_PHP tutorial
The tempnam() function creates a temporary file with a unique file name. If successful, the function returns the new temporary file name. On failure, returns false.
The tempnam() function creates a temporary file with a unique file name.
If successful, the function returns the new temporary file name. On failure, returns false.
Grammar
tempnam(dir,prefix)
*/
function dir_wriable($dir) //Custom function extension creates temporary file
{
$test=tempnam("$dir","test_file"); //Create a temporary file
if(fopen($test,"w">$fp=@fopen($test,"w")) //If the file is opened successfully
{
@fclose($fp); //Close the file
@unlink($test); //Delete file
$wriable="ture"; //The return value is true
}
else
{
$wriable=false or die("cannot open $test!"); //The return value is false
}
Return $wriable; //Return Boolean value
}
if(dir_wriable(str_replace('//','/',dirname(__file__)))) //Call custom function
{
$dir_wriable='File created successfully';
}
else
{
$dir_wriable='Failed to create file';
}
/*
If php tutorial cannot create the file in the specified dir parameter, it falls back to the system default value.
Note: The behavior of this function changed in version 4.0.3. A temporary file is also created to avoid a race condition where a file with the same name may exist on the file system between the time the string is generated as the file name and the time the script actually creates the file. Note that you need to delete this file if you no longer need it. It will not be deleted automatically.
The tmpfile() function creates a temporary file with a unique file name in read-write (w+) mode.
The file will be automatically deleted after closing (using fclose()), or when the script ends.
*/
$temp = tmpfile();
fwrite($temp, "testing, testing.");
//Rewind to the beginning of the file
rewind($temp);
//Read 1k from file
echo fread($temp,1024);
//Delete files
fclose($temp);