is_file 只判斷檔案是否存在;
| 代碼如下 |
複製代碼 |
<?php $file = "test.txt"; if(is_file($file)) { echo ("$file is a regular file"); }else { echo ("$file is not a regular file"); } ?>
輸出:test.txt is a regular file |
file_exists 判斷檔案是否存在或者是目錄是否存在;
| 代碼如下 |
複製代碼 |
<?php echo file_exists("test.txt"); ?> 輸入 1 |
is_dir 判斷目錄是否存在;
例子
| 代碼如下 |
複製代碼 |
<?php $file = "images"; if(is_dir($file)) { echo ("$file is a directory"); } else { echo ("$file is not a directory"); } ?> 輸出: images is a directory |
查看手冊,雖然這兩個函數的結果都會被緩衝,但是is_file卻快了N倍。
還有一個值得注意的:
檔案存在的情況下,is_file比file_exists要快N倍;
檔案不存在的情況下,is_file比file_exists要慢;
結論是,file_exits函數並不會因為該檔案是否真的存在而影響速度,但是is_file影響就大了
測試
| 代碼如下 |
複製代碼 |
檔案存在(目前的目錄) is_file:0.4570ms file_exists:2.0640ms 檔案存在(絕對路徑3層/www/hx/a/) is_file:0.4909ms file_exists:3.3500ms 檔案存在(絕對路徑5層/www/hx/a/b/c/) is_file:0.4961ms file_exists:4.2100ms 檔案不存在(目前的目錄) is_file:2.0170ms file_exists:1.9848ms 檔案不存在(絕對路徑5層/www/hx/a/b/c/) is_file:4.1909ms file_exists:4.1502ms 目錄存在 file_exists:2.9271ms is_dir:0.4601ms 目錄不存在 file_exists:2.9719ms is_dir:2.9359ms |