Php 简明教程

PHP - Open File

PHP 的内置函数库提供了 fopen() 函数来打开一个文件或任何其他流,并返回其“引用指针”,也称为“句柄”。

PHP’s built-in function library provides fopen() function to open a file or any other stream and returns its "reference pointer", also called as "handle".

PHP 中的 fopen() 函数类似于 C 中的 fopen(),只不过在 C 中,它无法打开 URL。

The fopen() function in PHP is similar to fopen() in C, except that in C, it cannot open a URL.

Syntax of fopen()

fopen() 函数具有以下签名 -

The fopen() function has the following signature −

fopen(
   string $filename,
   string $mode,
   bool $use_include_path = false,
   ?resource $context = null
): resource|false

参数 $filename$mode 是必需的。以下是参数说明 -

The $filename and $mode parameters are mandatory. Here’s the explanation of the parameters −

  1. $filename − This parameter is a string representing the resource to be opened. It may be a file in the local filesystem, or on a remote server with the scheme:// prefix.

  2. $mode − A string that represents the type of access given to the file/resource.

  3. $use_include_path − A Boolean optional parameter can be set to '1' or true if you want to search for the file in the include_path, too.

  4. $context − A context stream resource.

Modes of Opening a File

PHP 允许文件以以下模式打开 -

PHP allows a file to be opened in the following modes −

Modes

Description

r

Open a file for read only.

w

Open a file for write only. creates a new file even if it exists.

a

Open a file in append mode

x

Creates a new file for write only.

r+

Open a file for read/write.

w+

Open a file for read/write. creates a new file even if it exists.

a+

Open a file for read/write in append mode.

x+

Creates a new file for read/write.

c

Open the file for writing, if it doesn’t exist. However, if it exists, it isn’t truncated (as in w mode).

c++

Open the file for read/write, if it doesn’t exist. However, if it exists, it isn’t truncated (as in w mode).

e

Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems.

如果 fopen() 函数执行成功,则会返回绑定到文件流的“文件指针”或“句柄”资源。不过,如果失败,则会返回 FALSE,并发出 E_WARNING。

If the fopen() function is successfully executed, it returns a "file pointer" or "handle" resource bound to the file stream. However, if it fails, it returns false with E_WARNING being emitted.

$handle = fopen('a.txt, 'r');
var_dump($handle);

如果文件存在于当前目录中,则由 output 显示成功 −

If the file exists in the current directory, the success is shown by the output

resource(5) of type (stream)

如果不显示,则会得到以下 error message

If not, you get the following error message

Warning: fopen(a.txt): Failed to open stream:
No such file or directory in a.php on line 2
bool(false)

Examples

以下示例显示了 fopen() 函数的不同用法 −

The following examples show different usages of the fopen() function −

<?php
   $handle = fopen("hello.txt", "w");
   $handle = fopen("c:/xampp/htdocs/welcome.png", "rb");
   $handle = fopen("http://localhost/hello.txt", "r");
?>

请注意,当文件名是目录时,此函数也可能成功。在这种情况下,您可能需要使用 is_dir() 函数检查它是否是在执行任何读/写操作之前是一个文件。

Note that this function may also succeed when the filename is a directory. In that case, you may need to use the is_dir() function to check whether it is a file before doing any read/write operations.

一旦打开文件,就可以使用 fwrite()fputs() 等函数在其中写入数据,并使用 fread()fgets() 函数从中读取数据。

Once a file is opened, you can write data in it with the help of functions such as fwrite() or fputs(), and read data from it with fread() and fgets() functions.

Closing a File

始终建议关闭句柄引用的打开流 −

It is always recommended to close the open stream referenced by the handle −

fclose($handle);