Is_file determines whether the file exists and checks whether the specified filename is a normal file;
File_exists determine whether the file exists or whether the directory exists;
Is_dir determine whether the directory exists;
Look at the manual, although the results of both functions are cached, but the is_file is almost n times faster.
There is one more notable:
In the case of file existence, Is_file is faster n times than file_exists;
If the file does not exist, Is_file is slower than file_exists;
The conclusion is that the File_exits function does not affect speed because the file really exists, but the is_file effect is large.
The first question to ask is, is_file really can replace file_exists use it? The answer is in the negative. Why? The reason is simple, is_file has a cache
We can test it with the following code:
Copy the Code code 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 you run the test code, we make sure that the Test.txt file exists. In the above code, the first time you use the Is_file function to determine if a file exists, and then call the sleep function for 10 seconds. In this 10 seconds, we want to delete the Test.txt file. Finally look at the result of the second call to the Is_file function. The output results are as follows:
Test.txt exists!
Test.txt exists!
Well, you are not mistaken, both are output "test.txt exists!", this is why? The reason is that Is_file has a cache. When the Is_file function is called for the first time, PHP saves the file's properties, and when the is_file is called again, the cache is returned directly if the file name is the same as the first time.
What about changing is_file to file_exists? We can change the Is_file function of the above code to the File_exists function and test it again using the test method above. The results are as follows:
Test.txt exists!
Test.txt No exists!
The second call to File_exists when the return file does not exist, this is because the file_exists function is not cached, no time to call File_exists will go to the disk to search for the existence of the file, so the second time will return false.
Said so much, I just want to explain is_file can not replace file_exists use, if you just think is_file performance good, then I can't
The above introduces the differences in PHP is_file,file_exists, is_file can not replace the reasons for file_exits, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.