In PHP, is_file cannot replace file_exists. Run the following code to test the code :? Php=filenametest.txt; if (is_file ($ filename) {echo $ filenameexists! N;} else {echo $ filena can be tested using the following 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 a file exists, and then the sleep function is called for 10 seconds. In this 10 seconds, we will delete the test.txt file. Finally, let's look at the result of the second call to the is_file function. The output result is as follows:
Test.txt exists!
Test.txt exists!
Sorry, you didn't see the error. Both are output test.txt exists !", Why? The reason is that is_file is cached. When the is_file function is called for the first time, PHP saves the file attribute (file stat). when is_file is called again, if the file name is the same for the first time, then the cache is directly returned.
So how about changing is_file to file_exists? We can change the is_file function in the code above to the file_exists function, and use the above test method again. The result is as follows:
Test.txt exists!
Test.txt no exists!
When file_exists is called for the second time, the returned file does not exist because the file_exists function is not cached. when file_exists is called for the second time, the system searches for the existence of the file on the disk. Therefore, the system returns false for the second time.
I just want to explain that is_file cannot be used in place of file_exists. if you think is_file is of good performance, I cannot
The pipeline code is as follows :? Php $ filename = 'test.txt '; if (is_file ($ filename) {echo "$ filename exists! \ N ";}else {echo" $ filena...