Why does is_file in PHP not replace file_exists? PHP Tutorial

Source: Internet
Author: User
Tags sleep function
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.