Why the Is_file () function is not a substitute for the file_exists () function

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

Related Article

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.