(PHP 4, PHP 5)
file_exists — 檢查文件或目錄是否存在
說明
bool file_exists ( string$filename
)
檢查文件或目錄是否存在。
參數
filename
文件或目錄的路徑。
在 Windows 中要用 //computername/share/filename 或者 \computernamesharefilename 來檢查網絡中的共享文件。
返回值
如果由 filename
指定的文件或目錄存在則返回
TRUE
,否則返回 FALSE
。
Note:
This function will return FALSE
for symlinks pointing to non-existing
files.
如果因為安全模式的限制而導致不能訪問文件的話,該函數會返回
FALSE
。然而,可以使用
include 來包含,如果文件在
safe_mode_include_dir
所指定的目錄里。
Note:
The check is done using the real UID/GID instead of the effective one.
Note: 因為 PHP 的整數類型是有符號整型而且很多平臺使用 32 位整型,對 2GB 以上的文件,一些文件系統函數可能返回無法預期的結果 。
范例
Example #1 測試一個文件是否存在
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
錯誤/異常
失敗時拋出E_WARNING
警告。
注釋
Note: 此函數的結果會被緩存。參見 clearstatcache() 以獲得更多細節。
Tip自 PHP 5.0.0 起, 此函數也用于某些 URL 包裝器。請參見 支持的協議和封裝協議以獲得支持 stat() 系列函數功能的包裝器列表。
參見
is_readable() - 判斷給定文件名是否可讀 is_writable() - 判斷給定的文件名是否可寫 is_file() - 判斷給定文件名是否為一個正常的文件 file() - 把整個文件讀入一個數組中