PHP 中 file_exists 与 is_file,is_dir 的区别说明
很显然
file_exists
是受了 asp 的影响,因为 asp 不但有 fileExists 还有
folderExists,driverExists,那么 PHP 中
file_exists
是什么意思呢
很显然
file_exists
是受了 asp 的影响,因为 asp 不但有 fileExists 还有
folderExists,driverExists,那么 PHP 中
file_exists
是什么意思呢?
PHP
的
file_exists
=
is_dir
+
is_file
它既可以判断文件是否存在,又可以判断目录是否存在。但这样一个全面的函数执行效率
非常低,就像 asp 中 request 不指定是 form,还是 get,cookies
,所以结论是:
◦如果要判断目录是否存在,请用独立函数
is_dir
(directory)
◦如果要判断文件是否存在,请用独立函数
is_file
(filepath)
is_file
只判断文件是否存在;
file_exists
判断文件是否存在或者是目录是否存在;
is_dir
判断目录是否存在;
查看手册,虽然这两个函数的结果都会被缓存,但是
is_file
却快了 N
倍。
还有一个值得注意的:
文件存在的情况下,
is_file
比
file_exists
要快 N
倍;
文件不存在的情况下,
is_file
比
file_exists
要慢;
结论是,file_exits 函数并不会因为该文件是否真的存在而影响速度,但是
is_file
影响就
大了。