Home > Backend Development > PHP Tutorial > The difference between is_file and file_exists in PHP, the reason why is_file cannot replace file_exits

The difference between is_file and file_exists in PHP, the reason why is_file cannot replace file_exits

WBOY
Release: 2016-07-28 08:28:22
Original
1428 people have browsed it

is_file determines whether the file exists and checks whether the specified file name is a normal file;

file_exists determines whether the file or directory exists;

is_dir determines whether the directory exists;

Check the manual. Although the results of these two functions will be cached, is_file is N times faster.

There is another thing worth noting:

When the file exists, is_file is N times faster than file_exists;

When the file does not exist, is_file is slower than file_exists;

The conclusion is that the file_exits function does not Whether the file really exists affects the speed, but is_file has a greater impact.


The first question to ask is, can is_file really be used instead of file_exists? the answer is negative. Why? The reason is simple, is_file is cached

We can use the following code to test it:

Copy the codeThe 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";
                                           using using using       using using using '’’’’ using '' '' echo "'' ‑                      out                  out          out  out out out out out out out out through through out '’’’’’’’’’’’’’’’’’’’ out through out's' out out through out out out out out out out out out out out out out out clean out out outole out clean out out out out out out clean out out out out right clean out out out out out out so so so so so so so so so so so so so to to to to so to to To to to to To To His. In the above code, the is_file function is used for the first time to determine whether the file exists, and then the sleep function is called to sleep for 10 seconds. Within these 10 seconds, we need to delete the test.txt file. Finally, look at the result of calling the is_file function for the second time. The output results are as follows:
test.txt exists!
test.txt exists!
Well, you read that right, “test.txt exists!” was output twice. Why is this? The reason is that is_file has cache. When the is_file function is called for the first time, PHP will save the file attributes (file stat). When is_file is called again, if the file name is the same as the first time, it will be returned directly to the cache.

What about changing is_file to file_exists? We can change the is_file function in the above code to the file_exists function and use the above test method again to test. The results are as follows:
test.txt exists!
test.txt no exists!
The second time you call file_exists, it returns that the file does not exist. This is because the file_exists function is not cached. Every time you call file_exists, it will search the disk to see if the file exists. , so it will return false only the second time.
Having said so much, I just want to explain that is_file cannot be used instead of file_exists. If you insist that is_file has better performance, then I can’t help it


The above introduces the difference between is_file and file_exists in PHP. The reason why is_file cannot replace file_exits is included. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template