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

怪我咯
Release: 2023-03-13 13:56:01
Original
10276 people have browsed it

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;
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!