Php 简明教程

PHP - File Existence

在对文件执行任何处理之前,经常实用的是检查你现在尝试打开的文件是否确实存在。如果不是,程序可能会发出 runtime exception

It is often handy to check if the file you are trying to open really exists in the first place before performing any processing on it. Otherwise, the program is likely to raise a runtime exception.

PHP 的内置函数库在这方面提供了一些实用函数。本章中讨论的某些函数有 −

PHP’s built-in library provides some utility functions in this regard. Some of the functions we shall discuss in this chapter are −

  1. file_exists() − tests if the file exists

  2. is_file() − if the handle returned by the fopen() refers to a file or directory.

  3. is_readable() − test if the file you have opened allows reading data

  4. is_writable() − test if writing data in the file is allowed

The file_exists() Function

此函数适用于文件和目录。它检查给定的文件或目录是否存在。

This function works with a file as well as a directory. It checks whether the given file or directory exists or not.

file_exists(string $filename): bool

此函数的唯一参数是一个字符串,代表带有完整路径的文件/目录。此函数将根据文件是否存在返回真或假。

The only parameter to this function is a string representing the file/directory with full path. The function returns true or false depending upon the file exists or not.

Example

以下程序检查文件 “hello.txt” 是否存在。

The following program checks if the file "hello.txt" exists or not.

<?php
   $filename = 'hello.txt';
   if (file_exists($filename)) {
      $message = "The file $filename exists";
   } else {
      $message = "The file $filename does not exist";
   }
   echo $message;
?>

如果当前目录中存在文件,则消息为 −

If the file does exist in the current directory, the message is −

The file hello.txt exists

如果没有,则消息为 −

If not, the message is −

The file hello.txt does not exist

Example

指向文件的字符串可能具有相对路径或绝对路径。假设 “hello.txt” 文件可在当前目录中的 “hello” 子目录中找到。

The string pointing to the file may have a relative or absolute path. Assuming that "hello.txt" file is available in a "hello" subdirectory which is inside the current directory.

<?php
   $filename = 'hello/hello.txt';
      if (file_exists($filename)) {
   $message = "The file $filename exists";
   } else {
      $message = "The file $filename does not exist";
   }
   echo $message;
?>

它将生成以下 output

It will produce the following output

The file hello/hello.txt exists

Example

尝试如下指定绝对路径 −

Try giving the absolute path as below −

<?php
   $filename = 'c:/xampp/htdocs/hello.txt';
   if (file_exists($filename)) {
      $message = "The file $filename exists";
   } else {
      $message = "The file $filename does not exist";
   }
   echo $message;
?>

它将生成以下 output

It will produce the following output

The file c:/xampp/htdocs/hello.txt exists

The is_file() Function

file_exists() 函数为现有文件和目录返回 trueis_file() 函数可帮助您确定是否为文件。

The file_exists() function returns true for existing file as well as directory. The is_file() function helps you to determine if it’s a file.

is_file ( string $filename ) : bool

以下示例展示了 is_file() 函数的工作原理 −

The following example shows how the is_file() function works −

<?php
   $filename = 'hello.txt';

   if (is_file($filename)) {
      $message = "$filename is a file";
   } else {
      $message = "$filename is a not a file";
   }
   echo $message;
?>

output 表明它是一个文件 −

The output tells that it is a file −

hello.txt is a file

现在,将 “$filename” 更改为目录并查看结果 −

Now, change the "$filename" to a directory, and see the result −

<?php
   $filename = hello;

   if (is_file($filename)) {
      $message = "$filename is a file";
   } else {
      $message = "$filename is a not a file";
   }
   echo $message;
?>

现在,您将得知 “hello” 不是文件。

Now you will be told that "hello" is not a file.

请注意, is_file() 函数接受 $filename ,仅在 $filename 为文件且存在时返回 true

Note that The is_file() function accepts a $filename and returns true only if the $filename is a file and exists.

The is_readable() Function

有时,您可能希望事先检查是否可以读取文件。 is_readable() 函数可以确定此事实。

Sometimes, you may want to check before hand if the file can be read from or not. The is_readable() function can ascertain this fact.

is_readable ( string $filename ) : bool

Example

下面是 is_readable() 函数工作原理的示例 −

Given below is an example of how the is_readable() function works −

<?php
   $filename = 'hello.txt';
   if (is_readable($filename)) {
      $message = "$filename is readable";
   } else {
      $message = "$filename is not readable";
   }
   echo $message;
?>

它将生成以下 output

It will produce the following output

hello.txt is readable

The is_writable() Function

您可以使用 is_writable() 函数检查文件是否存在以及是否可以在给定文件上执行写操作。

You can use the is_writable() function to can check if a file exists and if it is possible to perform write operation on the given file.

is_writable ( string $filename ) : bool

Example

以下示例展示了 is_writable() 函数的工作原理 −

The following example shows how the is_writable() function works −

<?php
   $filename = 'hello.txt';

   if (is_writable($filename)) {
      $message = "$filename is writable";
   } else {
      $message = "$filename is not writable";
   }
   echo $message;
?>

对于普通归档文件,程序指出它是可写的。但是,将其属性更改为 “read_only” 并运行程序。您现在获得 −

For a normal archived file, the program tells that it is writable. However, change its property to "read_only" and run the program. You now get −

hello.txt is writable