PHP 中的 is_file 或 file_exists
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/792899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is_file or file_exists in PHP
提问by Duncan Benoit
I need to check if a file is on HDD at a specified location ($path.$file_name).
我需要检查文件是否位于指定位置 ($path.$file_name) 的 HDD 上。
Which is the difference between is_file()and file_exists()functions and which is better/faster to use in PHP?
is_file()和file_exists()函数之间的区别是什么,在 PHP 中使用哪个更好/更快?
回答by hbw
is_file()will return falseif the given path points to a directory. file_exists()will return trueif the given path points to a valid file ordirectory. So it would depend entirely on your needs. If you want to know specificallyif it's a file or not, use is_file(). Otherwise, use file_exists().
is_file()false如果给定的路径指向目录,将返回。file_exists()将返回true给出的路径指向一个有效的文件或目录。所以这完全取决于你的需求。如果您想具体知道它是否是文件,请使用is_file(). 否则,使用file_exists().
回答by Lamy
is_file()is the fastest, but recent benchmark shows that file_exists()is slightly faster for me. So I guess it depends on the server.
is_file()是最快的,但最近的基准测试表明file_exists()对我来说稍微快一些。所以我想这取决于服务器。
My test benchmark:
我的测试基准:
benchmark('is_file');
benchmark('file_exists');
benchmark('is_readable');
function benchmark($funcName) {
$numCycles = 10000;
$time_start = microtime(true);
for ($i = 0; $i < $numCycles; $i++) {
clearstatcache();
$funcName('path/to/file.php'); // or 'path/to/file.php' instead of __FILE__
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "$funcName x $numCycles $time seconds <br>\n";
}
Edit: @Tivie thanks for the comment. Changed number of cycles from 1000 to 10k. The result is:
编辑:@Tivie 感谢您的评论。将周期数从 1000 更改为 10k。结果是:
when the file exists:
is_file x 10000 1.5651218891144 seconds
file_exists x 10000 1.5016479492188 seconds
is_readable x 10000 3.7882499694824 seconds
when the file does not exist:
is_file x 10000 0.23920488357544 seconds
file_exists x 10000 0.22103786468506 seconds
is_readable x 10000 0.21929788589478 seconds
当文件存在时:
is_file x 10000 1.5651218891144 秒
file_exists x 10000 1.5016479492188 秒
is_readable x 10000 3.7882499694824 秒
当文件不存在时:
is_file x 10000 0.23920488357544 秒
file_exists x 10000 0.22103786468506 秒
is_readable x 10000 0.21929788589478 秒
Edit: moved clearstatcache(); inside the loop. Thanks CJ Dennis.
编辑:移动 clearstatcache(); 循环内。谢谢 CJ 丹尼斯。
回答by Brad
Neither.
两者都不。
is_file() can return true if the file doesnt exist.
如果文件不存在,is_file() 可以返回 true。
file_exists() can return true if the file is a directory.
如果文件是目录,file_exists() 可以返回 true。
So if it needs to be a file and it needs to exist then you need both.
所以如果它需要是一个文件并且它需要存在,那么你需要两者。
Speed doesn't matter here because they are not the same. Use only one if only one function matters and it will be faster.
速度在这里并不重要,因为它们不一样。如果只有一个功能很重要,则只使用一个,这样会更快。

