As for file-related operations, you must have a very good understanding of them, and we will explain them in detail when we go to the relevant file operation functions in the manual in the future. Today, we’ll take a look at temporary files in PHP.
Gets the default temporary create file directory for PHP
For those of you who have studied the Linux operating system, the/TMP directory (C:\Windows\Temp\) is used to store temporary files on the system, so this directory is also called temporary file directory. Many applications store temporary files in this directory, including caches, scripts, and so on. PHP also points temporary files to this directory by default, where temporary files such as SESSION files are stored. It can be set in the php.ini file via sys_tmp_DIR.
Of course, in a dynamically running PHP program, you can also use a function to retrieve the current temporary file directory.
print_r(sys_get_temp_dir());
// /tmp
Copy the code
Create a temporary file
Now that we have a temporary file directory, PHP also has a function to create a temporary file directly.
$tmpFile = tmpfile();
fwrite($tmpFile."I'm tmp file.");
// ll /tmp
// vim phpbnAjbE
sleep(10);
fclose($tmpFile);
// ll /tmp
Copy the code
The tmpFile () function is used to create this temporary file. We do not need to specify a filename or a path for it, and it creates a file of type W +, which is directly readable and writable. The temporary file is automatically deleted when fclose() is called. The manual states that files created using this function will also be automatically deleted after the script is finished, but after testing, the files will not be deleted after the script is finished.
After calling the function and writing, we pause for ten seconds. To see if the file was generated successfully, go to/TMP. Based on when the file was created, we found the corresponding generated file. Then, after executing fclose() ten seconds later, go back to the directory and see that the file has been deleted automatically.
Creates a temporary file with a unique name based on the directory state
Finally, PHP provides a very user-friendly function for creating temporary files.
$tmpFile = tempnam('/Users/zhangyue/MyDoc/ blog-blog/PHP /202006/source'.'testtmp');
$f = fopen($tmpFile."w");
fwrite($f."I'm tmp file.");
// ll /tmp
// vim testtmpH7bptZ
The // etc directory has no write permission
$tmpFile = tempnam('/etc'.'testtmp');
$f = fopen($tmpFile."w");
fwrite($f."I'm tmp file.");
// ll /etc
// ll /tmp
// vim testtmpTUNucM
Copy the code
The tempnam() function, which generates a temporary file with a unique name based on the directory state. What do you mean by directory state? The /etc directory in the second paragraph is usually the root directory. Without the root account permission, we cannot create or modify the file. If the directory has no permissions, or does not exist at all, the tempnam() function will generate the file into the temporary file directory. If the directory exists and is writable, as in the first code, the file will be created in that directory normally.
The second argument to the tempnam() function is to specify the prefix of the generated file name. The tmpFile () function does not specify a file name, but gives a fixed prefix to the file name and guarantees that the auto-generated part of the file name after the prefix is unique.
conclusion
Here are some interesting functions that are useful for temporary file manipulation, whether it’s for temporary caching or for creating some files. We can try using these functions more often in future development, and they may give us a lot of productivity gains.
Test code:
Github.com/zhangyue050…
Reference Documents:
www.php.net/manual/zh/f…
www.php.net/manual/zh/f…
www.php.net/manual/zh/f…