PHP Learning Path: PHP is_file and File_exist differences, and determine the directory Is_dir

Source: Internet
Author: User
Tags sleep function
In PHP, Is_file and file_isexist have a very small community.
1) Is_file:
$path = "/path/to/file/text.txt";
if (file_exists ($path))
echo "File Exists";
Else
echo "File not Exists";
For example, in this example, the file exists will return true, there is no return false, but note that if you pass in a
The correct path (such as a file directory) also returns true:
$path = "/path/to/file";

2) File_isexist ()
This will not be, really is to determine whether a file exists, if passed in the above one although it seems

The correct path is also the return false OH


Read this PHP file_exists and is_file,is_dir The difference between the basic understanding, PHP file_exists = Is_dir + is_file.

Write the program to verify:

Execute 1000 times and record the time required.

File exists (current directory)
Is_file:0.4570ms
File_exists:2.0640ms

File exists (absolute path 3 layer/www/hx/a/)
Is_file:0.4909ms
File_exists:3.3500ms

File exists (absolute path 5 layer/www/hx/a/b/c/)
Is_file:0.4961ms
File_exists:4.2100ms

File does not exist (current directory)
Is_file:2.0170ms
File_exists:1.9848ms

File does not exist (absolute path 5 layer/www/hx/a/b/c/)
Is_file:4.1909ms
File_exists:4.1502ms

Directory exists
File_exists:2.9271ms
Is_dir:0.4601ms
Directory does not exist
File_exists:2.9719ms
Is_dir:2.9359ms

Is_file ($file)
File_exists ($file)
when $file is a directory, Is_file returns false,file_exists true

In the case of a file, Is_file is much faster than file_exists;
The deeper the directory in which the file is to be detected, the greater the speed difference, but at least 4 times times faster.

If the file does not exist, Is_file is a little slower than file_exists, but it can be ignored.

In the case of directory existence, Is_dir is much faster than file_exists;
If the directory does not exist, Is_dir is a little slower than file_exists, but it can be ignored.

Conclusion:
if you want to determine if a file exists, use the function is_file (),
If you want to determine if the directory exists, use the function Is_dir (),
It seems that there is no place to use file_exists, not sure if the parameters passed in file or directory?

We can test it with the following code:

Copy the code code 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 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 are not mistaken, 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.

What about changing 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, no time to call File_exists will go to the disk to search for the existence of the file, so the second time will return false.

Said so much, I just want to explain is_file can not replace file_exists use, if you just think is_file performance good, then I can't

  • 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.