Home > Article > Backend Development > php sys_get_temp_dir - Returns the temporary file path_PHP tutorial
Okay, without further ado, let’s take a look at the usage and implementation principle of this php sys_get_temp_dir - return temporary file path function.
sys_get_temp_dir
(PHP 5" = 5.2.1)
Okay, without further ado, let’s take a look at the usage and implementation principle of this php sys_get_temp_dir - return temporary file path function.
sys_get_temp_dir - Returns the directory path for temporary files
Description
String sys_get_temp_dir (void)
Returns the directory path to the PHP store's temporary files by default.
Return value
Returns the path to the temporary directory.
Example
Example #1 Example of sys_get_temp_dir()
Examples
Example #1 sys_get_temp_dir() example
// Create a temporary file in the temporary
// files directory using sys_get_temp_dir()
$temp_file = tempnam(sys_get_temp_dir(), 'Tux');
echo $temp_file;
?>
The above example will output something similar to:
C:WindowsTempTuxA318.tmp
How to implement this function:
if ( !function_exists('sys_get_temp_dir')) {
function sys_get_temp_dir() {
If (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
If (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
If (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
$tempfile=tempnam(uniqid(rand(),TRUE),'');
If (file_exists($tempfile)) {
Unlink($tempfile);
Return realpath(dirname($tempfile));
}
}
}
?>