is_file determines whether the file exists and checks whether the specified file name is a normal file;
file_exists determines whether the file or directory exists;
is_dir determines whether the directory exists;
Check the manual. Although the results of these two functions will be cached, is_file is N times faster.
There is another thing worth noting:
When the file exists, is_file is N times faster than file_exists;
When the file does not exist, is_file is slower than file_exists;
The conclusion is that the file_exits function does not Whether the file really exists affects the speed, but is_file has a greater impact.
The first question to ask is, can is_file really be used instead of file_exists? the answer is negative. Why? The reason is simple, is_file is cached
We can use the following code to test it:
Copy the codeThe 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";
using using using using using using '’’’’ using '' '' echo "'' ‑ out out out out out out out out out out out through through out '’’’’’’’’’’’’’’’’’’’ out through out's' out out through out out out out out out out out out out out out out out clean out out outole out clean out out out out out out clean out out out out right clean out out out out out out so so so so so so so so so so so so so to to to to so to to To to to to To To His. 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 results are 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.