is_file、is_dir和file_exists用途和效率比较

来源:互联网 发布:淘宝的商务模式分析 编辑:程序博客网 时间:2024/06/16 02:40

转载于:http://blog.csdn.net/xiao7702/article/details/40406189


一、用途方面

$file = './test.php';

$dir = 'c:/wamp/www/test/';

file_exists($file);  //如果文件test.php存在,返回true 反之返回false

file_exists($dir);  //如果test目录存在,返回true,返回true 反之返回false


is_file($file);  //如果文件test.php存在,返回true 反之返回false

is_file($dir);  //无论目录存在否都返回false


二、处理速度方面

测试结果:

当文件存在时:
运行1万次:
is_file–>0.0067121982574463
file_exits–>0.11532402038574

运行10万次:
is_file–>0.069056034088135
file_exits–>1.1521670818329

当运行100万次:
is_file–>0.6924250125885
file_exits–>11.497637987137

当文件不存在时:

运行1万次:
is_file–>0.72184419631958
file_exits–>0.71474003791809

运行10万次:
is_file–>7.1535291671753
file_exits–>7.0911409854889

当运行100万次:
is_file–>72.042867183685
file_exits–>71.789203166962

超过1分钟了,别忘了在php第一行加句:
set_time_limit(120);//时间限制120秒

结论:
当文件存在时,is_file函数比file_exists函数速度快14倍,当文件不存在时,两者速度相当。
当文件目录存在时,is_dir()比file_exists()快18倍,不存在时两者效率相当。PHP的file_exists = is_dir + is_file。


* 如果要判断目录是否存在,请使用函数 is_dir(directory)
* 如果要判断文件是否存在,请使用函数 is_file(filepath)
* 判断目录或文件是否存在,请使用函数file_exists($path)

原创粉丝点击