用 PHP 内置函数 file_exists 可以检查某个文件或目录是否存在。如果文件或目录存在,file_exists 函数返回 TRUE,如果不存在,则返回 FALSE。
下面是一个简单的检查文件是否存在的实例代码:
<html> <body> <?php $filename = "C:\\blabla\\php\\hello.txt"; if (file_exists($filename)) {echo "The file $filename exists.";} else {echo "The file $filename does not exist.";} ?> </body> </html>
如果文件存在,执行该 PHP 文件的显示结果是:
The file C:\blabla\php\hello.txt exists.
如果文件不存在,执行该 PHP 文件的显示结果是:
The file C:\blabla\php\hello.txt does not exist.
你也可以用file_exists 函数测试某个目录是否存在,示例代码如下:
if (file_exists("C:\\blabla\\php")) {echo "yes";} else {echo "no";}
|