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:
The code is as follows:
<?php $filename = ' test.txt '; if (Is_file ($filename)) { echo "$filename exists!\n"; } else { echo "$filename no exists!\n"; } Sleep (ten); 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 have not read the wrong, 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.
then change the 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, the time of the call file_exists will go to the disk to search for the existence of the file, so the second time will return false.
So is_file is not a substitute for file_exists.