PHP to determine whether the file exists using Is_file or file_exists? _php Tips

Source: Internet
Author: User

Do you use Is_file or file_exists to determine whether a file exists?

In writing procedures found in the determination of the existence of documents, there are two ways to write, some people used is_file, some people use file_exists, which better or more appropriate?

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

Write a program to verify:

Execute 1000 times separately to record the time required.

File exists (current directory)

Copy Code code as follows:

Is_file:0.4570ms
File_exists:2.0640ms

File exists (absolute path 3-layer/www/hx/a/)
Copy Code code as follows:

Is_file:0.4909ms
File_exists:3.3500ms

File exists (absolute path 5-layer/www/hx/a/b/c/)
Copy Code code as follows:

Is_file:0.4961ms
File_exists:4.2100ms

File does not exist (current directory)
Copy Code code as follows:

Is_file:2.0170ms
File_exists:1.9848ms

File does not exist (absolute path 5-layer/www/hx/a/b/c/)
Copy Code code as follows:

Is_file:4.1909ms
File_exists:4.1502ms

Directory exists
Copy Code code as follows:

File_exists:2.9271ms
Is_dir:0.4601ms

Directory does not exist
Copy Code code as follows:

File_exists:2.9719ms
Is_dir:2.9359ms

Is_file ($file)
File_exists ($file)

Is_file returns False,file_exists returns True when $file is a directory

In the case of files, Is_file is much faster than file_exists;
The deeper the directory you want to detect, the worse the speed, but at least 4 times times faster.

If the file does not exist, the is_file is a little slower than the file_exists, but can be negligible.

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

Conclusion:

If you want to determine whether a file exists, use the function is_file (),
If you want to determine whether the directory exists, use the function Is_dir (),
There seems to be no place to use file_exists, not sure if the incoming parameter is a file or a directory?

Attached test procedure:

Copy Code code as follows:

function Runtime ($t 1) {
Return Number_format ((Microtime (True)-$t 1) *1000, 4). ' Ms ';
}
$times = 1000;

$t 1 = microtime (true);
for ($i =0; $i < $times; $i + +) {
Is_file ('/www/hx/www.9enjoy.com/config.php ');
}

Echo ' <br>is_file: '. Runtime ($t 1);

$t 2 = Microtime (true);
for ($i =0; $i < $times; $i + +) {
File_exists ('/www/hx/www.9enjoy.com/config.php ');
}
Echo ' <br>file_exists: '. Runtime ($t 2);

/*
$t 3 = Microtime (true);
for ($i =0; $i < $times; $i + +) {
Is_dir ('/www/hx/www.9enjoy.com/');
}
Echo ' <br>is_dir: '. Runtime ($t 3);
*/

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.