Php 简明教程

PHP - File Handling

在 PHP 中,文件是一种资源对象,可以按线性方式从其中读写数据。“文件处理”一词是指 PHP 中的一组函数,这些函数允许使用 PHP 代码对磁盘文件执行读/写操作。

In PHP, a file is a resource object, from which data can be read or written to in a linear fashion. The term "file handling" refers to a set of functions in PHP that enable read/write operations on disk files with PHP code.

一个文件对象会被分类为 stream 。可以在其上执行线性读/写操作的任何资源都是流。其他类似流的对象是 TCP 套接字、标准输入流,即由“php://stdin”表示的系统键盘、由“php://stdout”表示的标准输出流以及错误流“php://stderr”。

A file object is classified as a stream. Any resource on which linear read/write operations are done is a stream. Other stream-like objects are the TCP sockets, standard input stream, i.e., a system keyboard represented by "php://stdin", the standard output stream represented by "php://stdout", and the error stream "php://stderr".

Note − 常量 STDIN、STDOUT 和 STDERR 分别代表相应的标准流。

Note − Tthe constants STDIN, STDOUT, and STDERR stand for the respective standard streams.

尽管 PHP 被视为用于开发 Web 应用程序的服务器端脚本语言,但 PHP 还有一个命令行界面来执行控制台 IO 操作。

Although PHP is regarded as a server-side scripting language for developing web applications, PHP also has a command-line interface to perform console IO operations.

Example

PHP 中的 readline() 函数接受来自标准键盘的用户输入,echo/print 语句在控制台上呈现输出。

The readline() function in PHP accepts the user input from a standard keyboard, and echo/print statements render the output on the console.

<?php
   $str = readline("Type something:");
   echo $str;
?>

它将生成以下 output

It will produce the following output

C:\xampp\php>php hello.php
Type something: Are you enjoying this PHP tutorial?
Are you enjoying this PHP tutorial?

Example

我们可以通过从“php://stdin”读取输入并将其输出到“php://stdout”来获得相同的效果。

We can obtain the same effect by reading the input from "php://stdin" and outputting it to "php://stdout".

<?php
   $f = fopen("php://stdin", "r");
   echo "Type something: ";

   $str = fgets($f);
   $f1 = fopen("php://stdout", "w");

   fputs($f1, $str);
?>

这里,fopen() 函数用于打开 stdin 流以进行读取并打开 stdout 流以进行写入。

Here, the fopen() function is used to open the stdin stream for reading and the stdout stream for writing.

Example

PHP 支持各种流协议,以用于流相关函数,例如 fopen()、file_exists() 等。使用 php_get_wrappers() 函数获取所有已注册包装器的列表。

PHP supports a variety of stream protocols for stream related functions such as fopen(), file_exists(), etc. Use php_get_wrappers() function to get a list of all the registered wrappers.

<?php
   print_r(stream_get_wrappers());
?>

它将生成以下 output

It will produce the following output

Array
(
   [0] => php
   [1] => file
   [2] => glob
   [3] => data
   [4] => http
   [5] => ftp
   [6] => zip
   [7] => compress.zlib
   [8] => compress.bzip2
   [9] => https
   [10] => ftps
   [11] => phar
)

这些流被引用为“scheme://target”。例如,文件流为“file://xyz.txt”。

The streams are referenced as "scheme://target". For instance, the file stream is "file://xyz.txt".

在应用程序运行之前,来自控制台的输入数据存储在计算机的主内存(RAM)中。此后,来自 RAM 的内存内容会被擦除。

The input data from the console is stored in the computer’s main memory (RAM) until the application is running. Thereafter, the memory contents from RAM are erased.

我们希望以一种方式存储它,以便在永久介质(如磁盘文件)中需要时可以检索它。因此,我们将使用磁盘文件(而非标准流(键盘用于输入,显示设备用于输出))来读取数据,以及将数据存储的目的位置。

We would like to store it in such a way that it can be retrieved whenever required in a persistent medium such as a disk file. Hence, instead of the standard streams (keyboard for input and the display device for output), we will use the disk files for reading the data, and destination for storing the data.

除了上面示例中使用(使用标准流的 IO 操作)的读写模式外,还可以使用“r+”和“w+”(用于同时读写)、“b”(用于二进制模式)等各种其他模式打开文件流。

In addition to the read and write modes as used in the above example (IO operations with standard streams), the file stream can be opened in various other modes like "r+" and "w+" for simultaneous read/write, "b" for binary mode, etc.

要打开磁盘文件以便读取并获取其引用指针,请使用 fopen() 函数。

To open a disk file for reading and obtain its reference pointer, use the fopen() function.

$handle = fopen('file://' . __DIR__ . '/data.txt', 'r');

“file://”方案是默认方案。因此,可以很容易地将其删除,尤其是在处理本地文件时。

The "file://" scheme is the default. Hence, it can be easily dropped, especially when dealing with local files.

Note − 强烈建议关闭已打开的流。为此,请使用 fclose() 函数。

Note − It is always recommended to close the stream that was opened. Use the fclose() function for this purpose.

fclose($handle);

PHP 包含几个用于在文件流上执行读/写操作的内置函数。在后续的章节中,我们将探索文件系统函数。

PHP has several built-in functions for performing read/write operations on the file stream. In the subsequent chapters, we shall explore the filesystem functions.