Performance analysis of is_dir, is_file, file_exists functions in php

Source: Internet
Author: User
Php, is_dir, is_file, file_exists obviously file_exists is affected by asp, because asp not only has fileExists but also folderExists and driverExists, then file _

Php, is_dir, is_file, file_exists

Obviously, file_exists is affected by asp, because asp not only has fileExists but also folderExists and driverExists. what does file_exists in PHP mean?

File_exists = is_dir + is_file in PHP

It can determine whether a file exists and whether a directory exists, but such a comprehensive function execution efficiency is very low, just as the request in asp does not specify form or get, cookies, so the conclusion is:

To determine whether a directory exists, use the independent function is_dir (directory). to determine whether a file exists, use the independent function is_file (filepath)

Is_file only checks whether the file exists;

File_exists checks whether the file exists or whether the directory exists;

Is_dir: determines whether a directory exists;

Check the manual. although the results of both functions are cached, the is_file speed is N times faster. Note that when a file exists, is_file is N times faster than file_exists. if the file does not exist, is_file is slower than file_exists;

Conclusion:The file_exits function does not affect the speed because the file actually exists, but the is_file function has a big impact. I have understood the differences between file_exists and is_file and is_dir in PHP, file_exists = is_dir + is_file of PHP.

Write a program to verify,Execute 1000 times, record the required time, the code is as follows, the file exists (current directory)

  1. Is_file: 0.4570 ms
  2. File_exists: 2.0640 ms
  3. File exists (absolute path layer 3/www/hx//)
  4. Is_file: 0.4909 ms
  5. File_exists: 3.3500 ms
  6. File exists (absolute path layer 5/www/hx/a/B/c /)
  7. Is_file: 0.4961 ms
  8. File_exists: 4.2100 ms
  9. File does not exist (current directory)
  10. Is_file: 2.0170 ms
  11. File_exists: 1.9848 ms
  12. File does not exist (absolute path layer 5/www/hx/a/B/c /)
  13. Is_file: 4.1909 ms
  14. File_exists: 4.1502 ms
  15. Directory exists
  16. File_exists: 2.9271 ms
  17. Is_dir: 0.4601 ms
  18. The directory does not exist.
  19. File_exists: 2.9719 ms
  20. Is_dir: 2.9359 ms
  21. Is_file ($ file)
  22. File_exists ($ file)

When $ file is a directory, is_file returns false, and file_exists returns true. if the file exists, is_file is much faster than file_exists. the deeper the directory where the file is located, the more the speed difference, but at least four times faster. If the file does not exist, is_file is a little slower than file_exists, but it is negligible. If a directory exists, is_dir is much faster than file_exists. if a directory does not exist, is_dir is a little slower than file_exists, but can be ignored.

Conclusion:If you want to determine whether a file exists, use the is_file () function. if you want to determine whether a directory exists, use the is_dir () function. it seems that file_exists is not needed, are you sure you want to use the input parameters in a file or directory?

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.