array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.file-exists.php', 1 => 'file_exists', ), 'up' => array ( 0 => 'ref.filesystem.php', 1 => '文件系统函数', ), 'prev' => array ( 0 => 'function.fgetss.php', 1 => 'fgetss', ), 'next' => array ( 0 => 'function.file-get-contents.php', 1 => 'file_get_contents', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/filesystem/functions/file-exists.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
file_exists — 检查文件或目录是否存在
filename
文件或目录的路径。
在 Windows 中要用 //computername/share/filename 或者 \\computername\share\filename 来检查网络中的共享文件。
如果由 filename
指定的文件或目录存在则返回
true
,否则返回 false
。
注意:
对于指向文件不存在的符号链接,此函数将会返回
false
。
注意:
The check is done using the real UID/GID instead of the effective one.
注意: 因为 PHP 的整数类型是有符号整型而且很多平台使用 32 位整型,对 2GB 以上的文件,一些文件系统函数可能返回无法预期的结果。
失败时抛出 E_WARNING
警告。
示例 #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
警告。