PHP 中的 file_exists() 函数

phpprogrammingserver side programming

file_exists 方法检查文件或目录是否存在。它接受要检查的文件或目录的路径作为参数。以下是它的用途 −

  • 当您需要在处理之前知道文件是否存在时,它很有用。

  • 因此,在创建新文件时使用此函数可以知道文件是否已经存在。

语法

file_exists($file_path)

参数

  • file_path − 设置要检查是否存在的文件或目录的路径。必需。

返回

file_exists() 方法返回。

  • 如果文件或目录存在,则为 True
  • 如果文件或目录不存在,则为 False存在

示例

让我们看一个示例,该示例检查"candidate.txt"文件,即使文件不存在也会显示一条消息。

<?php
$myfile = '/doc/candidate.txt';
if (file_exists($myfile)) {
   echo "$myfile 存在!";
} else {
   echo "$myfile 不存在!";
}
?>

以下是显示文件不存在的输出。

输出

/doc/candidate.txt 不存在!

相关文章