PHP如何使用file_exists()函数?用法详细解读

2021年4月3日19:17:05 发表评论 928 次浏览

PHP中的file_exists()函数是一个内置函数, 用于检查文件或目录是否存在

你要检查的文件或目录的路径作为参数传递给file_exists()函数, 该函数成功时返回True, 失败时返回False。

语法如下:

file_exists($path)

参数:PHP中的file_exists()函数仅接受一个参数$路径。它指定要检查的文件或目录的路径。

返回值:成功返回True, 失败返回False。

错误和异常:

  1. 如果指定的路径指向不存在的文件, 则file_exists()函数将返回False。
  2. 对于大于2gb的文件, 由于PHP的整数类型是带符号的并且许多平台使用32位整数, 因此某些文件系统功能可能会产生意外结果。

例子:

Input : echo file_exists('/user01/work/gfg.txt');
Output : 1

Input : $file_pointer = '/user01/work/gfg.txt';
        if (file_exists($file_pointer)) {
            echo "The file $file_pointer exists";
        }else {
            echo "The file $file_pointer does 
                                   not exists";
        }
Output : 1

下面的程序说明了file_exists()函数。

程序1:

<?php
  
// checking whether file exists or not
echo file_exists ( '/user01/work/gfg.txt' );
  
?>

输出如下:

1

程序2:

<?php
  
// checking whether file exists or not
$file_pointer = '/user01/work/gfg.txt' ;
  
if ( file_exists ( $file_pointer )) 
{
     echo "The file $file_pointer exists" ;
}
else 
{
     echo "The file $file_pointer does
                              not exists";
}
  
?>

输出如下:

1

参考:

http://php.net/manual/en/function.file-exists.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: