php中is_file和file_exists與is_dir的區別_PHP教程

來源:互聯網
上載者:User
本文章介紹了php中is_file和file_exists與is_dir的區別,有需要的朋友可參考一下。

is_file 只判斷檔案是否存在;

代碼如下 複製代碼


$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 判斷檔案是否存在或者是目錄是否存在;

代碼如下 複製代碼

echo file_exists("test.txt");
?>

輸入

1

is_dir 判斷目錄是否存在;

例子

代碼如下 複製代碼

$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

http://www.bkjia.com/PHPjc/631588.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631588.htmlTechArticle本文章介紹了php中is_file和file_exists與is_dir的區別,有需要的朋友可參考一下。 is_file 只判斷檔案是否存在; 代碼如下 複製代碼 ?php $file =...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.