(PHP 4, PHP 5, PHP 7, PHP 8)
file_exists — 檢查文件或目錄是否存在
$filename
): bool檢查文件或目錄是否存在。
filename
文件或目錄的路徑。
在 Windows 中要用 //computername/share/filename 或者 \\computername\share\filename 來(lái)檢查網(wǎng)絡(luò )中的共享文件。
如果由 filename
指定的文件或目錄存在則返回
true
,否則返回 false
。
注意:
This function will return
false
for symlinks pointing to non-existing files.
注意:
The check is done using the real UID/GID instead of the effective one.
注意: 因為 PHP 的整數類(lèi)型是有符號整型而且很多平臺使用 32 位整型,對 2GB 以上的文件,一些文件系統函數可能返回無(wú)法預期的結果。
示例 #1 測試一個(gè)文件是否存在
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
失敗時(shí)拋出E_WARNING
警告。
注意: 此函數的結果會(huì )被緩存。參見(jiàn) clearstatcache() 以獲得更多細節。
自 PHP 5.0.0 起, 此函數也用于某些 URL 包裝器。請參見(jiàn) 支持的協(xié)議和封裝協(xié)議以獲得支持 stat() 系列函數功能的包裝器列表。