file_exists

(PHP 3, PHP 4, PHP 5)

file_exists -- 檢查檔案或目錄是否存在

說明

bool file_exists ( string filename )

若果由 filename 特殊的檔案或目錄存在則返回 TRUE,否則返回 FALSE

在 Windows 中要用 //computername/share/filename 或是 \\computername\share\filename 來檢查網路中的共享檔案。

例子 1. 測試一個檔案是否存在

<?php
$filename 
'/path/to/foo.txt';

if (
file_exists($filename)) {
    echo 
"The file $filename exists";
} else {
    echo 
"The file $filename does not exist";
}
?>

注: 本函數的結果會被暫存。更多訊息參見 clearstatcache()

提示: PHP 5.0.0 起本函數也可被某些 URL wrapper 使用。參考附錄 M 來看哪些 wrapper 支援 stat() 系列函數的功能。

警示

若果因為安全模式的限制而導致不能訪問檔案的話,該函數會返回 FALSE。然而,若果檔案在 safe_mode_include_dir 所特殊的目錄內的話,仍然可以使用 include 來包括。

參見 is_readable()is_writable()is_file()file()