We can use the following code to test it:
Copy the code The code is as follows:
$filename = 'test .txt';
if (is_file($filename)) {
echo "$filename exists!n";
} else {
echo "$filename no exists!n";
}
sleep(10);
if (is_file($filename)) {
echo "$filename exists!n";
} else {
echo "$filename no exists!n" ";
}
?>
When running the test code, we ensure that the test.txt file exists. In the above code, the is_file function is used for the first time to determine whether the file exists, and then the sleep function is called to sleep for 10 seconds. Within these 10 seconds, we need to delete the test.txt file. Finally, look at the result of calling the is_file function for the second time. The output result is as follows:
test.txt exists!
test.txt exists!
Well, you read that right, "test.txt exists!" was output twice. Why is this? The reason is that is_file has cache. When the is_file function is called for the first time, PHP will save the file attributes (file stat). When is_file is called again, if the file name is the same as the first time, it will be returned directly to the cache.
What about changing is_file to file_exists? We can change the is_file function in the above code to the file_exists function and use the above test method again to test. The results are as follows:
test.txt exists!
test.txt no exists!
When file_exists is called for the second time, it is returned that the file does not exist. This is because the file_exists function is not cached and will happen every time file_exists is called. Search the disk to see if the file exists, so it will return false only the second time.
Having said so much, I just want to point out that is_file cannot be used instead of file_exists. If you insist that is_file has better performance, then there is nothing I can do about it.
http://www.bkjia.com/PHPjc/736811.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736811.htmlTechArticleWe can use the following code to test it: Copy the code as follows: ?php $filename = 'test.txt' ; if (is_file($filename)) { echo "$filename exists!n"; } else { echo "$filena...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn