函式名:file_exists()
適用版本:所有版本
用法:file_exists() 函式用於檢查檔案或目錄是否存在。
語法:bool file_exists ( string $filename )
引數:
- $filename:要檢查的檔案或目錄的路徑。
返回值:
- 如果檔案或目錄存在,則返回 true。
- 如果檔案或目錄不存在,則返回 false。
示例:
// 檢查檔案是否存在
$file = 'path/to/file.txt';
if (file_exists($file)) {
echo "檔案存在";
} else {
echo "檔案不存在";
}
// 檢查目錄是否存在
$dir = 'path/to/directory';
if (file_exists($dir)) {
echo "目錄存在";
} else {
echo "目錄不存在";
}
注意事項:
- file_exists() 函式對於檔案和目錄都適用。
- 如果給定的路徑是一個符號連結,它將返回符號連結所指向的檔案或目錄是否存在。
- file_exists() 函式可以用來檢查遠端檔案的存在性,只需提供檔案的 URL 即可。