PHP to determine whether a file exists file_exists () function use detailed

Source: Internet
Author: User
Tags explode
Reading: In writing procedures found in the determination of the existence of files, there are two ways to write, some people use the is_file, some people use the file_exists, which is better or more appropriate? Does the file exist in Is_file or file_exists? When writing a program, it is found that there are two ways to determine if a file exists

However, the performance of these two functions is different, is_file () is faster than file_exists ().

If the file to be checked exists, then Is_file () is many times faster than file_exists (), but if the file does not exist, the two are similar.

The following is the result of the test, the first test is the file exists, and the second is a file that does not exist:

First Test, file present

<?php  //Run file_exists 10,000 times  $time = Microtime ();  $time = Explode (' ', $time);  $begintime = $time [1] + $time [0];  for ($i =0; $i <10000; $i + +)      file_exists ('/users/jacky ');         File exists  $time = Microtime ();  $time = Explode ("", $time);  $endtime = $time [1] + $time [0];  $totaltime = ($endtime-$begintime);  Echo ' runs file_exists 10,000 times: '. $totaltime. ' seconds '. Php_eol;      Run Is_file 10,000 times  $time = Microtime ();  $time = Explode ("", $time);  $begintime = $time [1] + $time [0];  for ($i =0; $i <10000; $i + +)      is_file ('/users/jacky ');  $time = Microtime ();  $time = Explode ("", $time);  $endtime = $time [1] + $time [0];  $totaltime = ($endtime-$begintime);  Echo ' runs Is_file 10,000 times: '. $totaltime. ' seconds. ' Php_eol;

Second Test, file does not exist

<?php//Run file_exists 10,000 times $time = Microtime ();  $time = Explode (' ', $time);  $begintime = $time [1] + $time [0];         for ($i =0; $i <10000; $i + +) file_exists ('/users/jackys ');  File does not exist $time = Microtime ();  $time = Explode ("", $time);  $endtime = $time [1] + $time [0];  $totaltime = ($endtime-$begintime); Echo ' runs file_exists 10,000 times: '. $totaltime. ' seconds '.      Php_eol;  Run Is_file 10,000 times $time = Microtime ();  $time = Explode ("", $time);  $begintime = $time [1] + $time [0];  for ($i =0; $i <10000; $i + +) is_file ('/users/jackys ');  $time = Microtime ();  $time = Explode ("", $time);  $endtime = $time [1] + $time [0];  $totaltime = ($endtime-$begintime); Echo ' runs Is_file 10,000 times: '. $totaltime. ' seconds. ' Php_eol; 

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.