In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to operate temporary files in PHP. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Get the default temporary creation file directory for PHP
Anyone who has studied the Linux operating system will know that there is a directory / tmp (usually C:\ Windows\ Temp\ in Windows), which is used to store some temporary files on the system, so this directory is also called the temporary files directory. Many software will put some temporary files in this directory, including some caches, some temporarily generated scripts and so on. By default, PHP also points the temporary files directory to this directory, where temporary files such as SESSION files are saved. It can be set through sys_tmp_dir in the php.ini file.
Of course, in dynamically running PHP programs, we can also use a function to get the current temporary file directory.
Print_r (sys_get_temp_dir ())
/ tmp
Create a temporary file
Now that we have a temporary file directory, PHP has kindly prepared a function for us to create a temporary file directly.
$tmpFile = tmpfile ()
Fwrite ($tmpFile, "I'm tmp file.")
/ / ll / tmp
/ / vim phpbnAjbE
Sleep (10)
Fclose ($tmpFile)
/ / ll / tmp
The tmpfile () function is used to create this temporary file. We do not need to specify a file name for it, nor do we need to specify a path for it. At the same time, the file it creates is of type w +, that is, a file that is directly readable and writable. When fclose () is called, the temporary file is automatically deleted. The manual says that files created using this function will also be automatically deleted after the end of the script, but after testing, it is found that the files will not be deleted at the end of the script.
After calling the function and writing the content, we paused for ten seconds. In fact, it is to go to the / tmp directory to see if the file is generated successfully. According to the time when the file was created, we found the corresponding file that was generated. Then after executing fclose () ten seconds later, check the directory again, and you will find that the file has been automatically deleted.
Create a temporary file with a unique name based on the directory status
Finally, PHP provides us with a very user-friendly function to create temporary files.
$tmpFile = tempnam ('/ Users/zhangyue/MyDoc/ blog post / dev-blog/php/202006/source', 'testtmp')
$f = fopen ($tmpFile, "w")
Fwrite ($f, "I'm tmp file.")
/ / ll / tmp
/ / vim testtmpH7bptZ
/ / etc directory does not have write permission
$tmpFile = tempnam ('/ etc', 'testtmp')
$f = fopen ($tmpFile, "w")
Fwrite ($f, "I'm tmp file.")
/ / ll / etc
/ / ll / tmp
/ / vim testtmpTUNucM
The tempnam () function, which generates a temporary file with a unique name based on the state of the directory. What do you mean according to the status of the directory? As you can see from the code comments above, the / etc directory in the second paragraph is generally a directory with root permissions, and we cannot create modification files without root account permissions. If it is such a directory that does not have permissions, or a directory that does not exist at all, the tempnam () function generates the files to a temporary file directory. If the directory is normal and writable, just like the first piece of code, the file will be created normally in this directory.
The second argument to the tempnam () function is to specify the prefix of the generated file name. The tmpfile () function cannot specify a file name, but this function gives the file name a fixed prefix and ensures that the automatically generated part of the file name after the prefix is unique.
PHP on how to operate on temporary files to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.