PHP function selection to determine whether a file exists (file_exists or is_file)

Source: Internet
Author: User
We know that PHP provides two built-in functions Is_file, file_exists, can be used in the appropriate situation, to determine whether the file exists, which is good, this article for everyone to make a simple explanation.

Look at the following test data, specific examples and see the data, is the most convincing oh.

Using Is_file and file_exists to detect the existence of files, respectively, 1000 times to record the time required.

File exists (current directory) is_file:0.4570msfile_exists:2.0640ms

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

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

File does not exist (current directory) is_file:2.0170msfile_exists:1.9848ms

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

Directory exists file_exists:2.9271msis_dir:0.4601ms directory does not exist FILE_EXISTS:2.9719MSIS_DIR:2.9359MS

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

In the case of a file, Is_file is much faster than file_exists, and the deeper the directory in which the file is located, 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 a directory, Is_dir is much faster than file_exists, and Is_dir is a little slower than file_exists in the absence of a directory, but negligible.

Believe, smart you, already see, Programmer's home for everyone to make a summary: 1, judge whether the file exists, with function Is_file (), 2, determine whether the directory exists, with the function Is_dir (), 3, not sure the parameters passed in the file or directory, you can consider the next file_exists , or else people are too lonely.

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