PHP--Determine if a file exists

Source: Internet
Author: User

File_exists

Is_file

Is_dir

Basically, php file_exists = is_dir + is_file

Write the program to verify:

Execute 1000 times and record the time required.

------------------------------------------------------------------------------------------------------

Is_file and file_exists

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

----------------------------------------------------------------------------------------

Is_dir and File_exists

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 (),

Not sure if the parameter passed in is a file or a directory, use file_exists.

PHP--Determine if a file exists

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.