我需要检查文件是否在指定位置的HDD上($ path.$ file_name).
哪个是is_file()和file_exists()函数之间的区别,哪个更好/更快在PHP中使用?
hbw*_*hbw 161
is_file()false如果给定路径指向目录,则返回.file_exists()将返回true给出的路径指向一个有效的文件或目录.所以这完全取决于你的需求.如果您想具体了解它是否是文件,请使用is_file().否则,请使用file_exists().
Lam*_*amy 35
is_file()是最快的,但最近的基准测试显示file_exists()对我来说稍微快一些.所以我想这取决于服务器.
我的测试基准:
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";
}
Run Code Online (Sandbox Code Playgroud)
编辑:@Tivie感谢您的评论.将周期数从1000改为10k.结果是:
当文件存在时:
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秒
编辑:移动clearstatcache(); 在循环内.谢谢CJ丹尼斯.
| 归档时间: |
|
| 查看次数: |
50600 次 |
| 最近记录: |