Home  >  Article  >  Backend Development  >  PHP determines whether a file exists. Detailed explanation of the use of the file_exists() function

PHP determines whether a file exists. Detailed explanation of the use of the file_exists() function

怪我咯
怪我咯Original
2017-07-11 10:46:1510338browse

Introduction: When writing a program, I found that there are two ways of writing when judging whether a file exists. Some people use is_file, and some people use file_exists. Which one is better or more suitable? To determine the existence of a file, use is_file or file_exists? When writing a program, I found that there are two methods to determine whether a file exists. However, the performance of these two functions is different. is_file() is faster than file_exists(). point.

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 about the same.

The following are the results of the test. The first test is that the file exists, and the second test is that the file does not exist:

The first test, the file exists

<?php  
// 运行 file_exists 10000 次  
$time = microtime();  
$time = explode(&#39; &#39;, $time);  
$begintime = $time[1] + $time[0];  
for($i=0;$i<10000;$i++)  
    file_exists(&#39;/Users/Jacky&#39;);         // 文件存在  
$time = microtime();  
$time = explode(" ", $time);  
$endtime = $time[1] + $time[0];  
$totaltime = ($endtime - $begintime);  
echo &#39;运行file_exists 10000 次所花时间: &#39; .$totaltime. &#39; 秒&#39;.PHP_EOL;  
  
  
// 运行 is_file 10000 次  
$time = microtime();  
$time = explode(" ", $time);  
$begintime = $time[1] + $time[0];  
for($i=0;$i<10000;$i++)  
    is_file(&#39;/Users/Jacky&#39;);  
$time = microtime();  
$time = explode(" ", $time);  
$endtime = $time[1] + $time[0];  
$totaltime = ($endtime - $begintime);  
echo &#39;运行 is_file 10000 次所花时间: &#39; .$totaltime. &#39; 秒.&#39;.PHP_EOL;

Second test, the file does not exist

<?php  
// 运行 file_exists 10000 次  
$time = microtime();  
$time = explode(&#39; &#39;, $time);  
$begintime = $time[1] + $time[0];  
for($i=0;$i<10000;$i++)  
    file_exists(&#39;/Users/Jackys&#39;);         // 文件不存在  
$time = microtime();  
$time = explode(" ", $time);  
$endtime = $time[1] + $time[0];  
$totaltime = ($endtime - $begintime);  
echo &#39;运行file_exists 10000 次所花时间: &#39; .$totaltime. &#39; 秒&#39;.PHP_EOL;  
  
  
// 运行 is_file 10000 次  
$time = microtime();  
$time = explode(" ", $time);  
$begintime = $time[1] + $time[0];  
for($i=0;$i<10000;$i++)  
    is_file(&#39;/Users/Jackys&#39;);  
$time = microtime();  
$time = explode(" ", $time);  
$endtime = $time[1] + $time[0];  
$totaltime = ($endtime - $begintime);  
echo &#39;运行 is_file 10000 次所花时间: &#39; .$totaltime. &#39; 秒.&#39;.PHP_EOL;

The above is the detailed content of PHP determines whether a file exists. Detailed explanation of the use of the file_exists() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn