Php 简明教程

PHP - Files & I/O

本章将说明与文件相关的以下函数 −

This chapter will explain following functions related to files −

  1. Opening a File

  2. Reading a File

  3. Writing a File

  4. Closing a File

Opening and Closing Files

PHP fopen() 函数用于打开一个文件。它需要两个参数,首先是文件名,然后是操作模式。

The PHP fopen() function is used to open a file. It requires two arguments stating first the file name and then mode in which to operate.

文件模式可以指定为此表中的六个选项之一。

Files modes can be specified as one of the six options in this table.

Sr.No

Mode & Purpose

1

r Opens the file for reading only. Places the file pointer at the beginning of the file.

2

r+ Opens the file for reading and writing. Places the file pointer at the beginning of the file.

3

w Opens the file for writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attempts to create a file.

4

w+ Opens the file for reading and writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attempts to create a file.

5

a Opens the file for writing only. Places the file pointer at the end of the file. If files does not exist then it attempts to create a file.

6

a+ Opens the file for reading and writing only. Places the file pointer at the end of the file. If files does not exist then it attempts to create a file.

如果打开文件失败,则 fopen 返回 false 的值,否则它返回一个 file pointer ,用于进一步读取或写入该文件。

If an attempt to open a file fails then fopen returns a value of false otherwise it returns a file pointer which is used for further reading or writing to that file.

在对打开的文件进行更改后,使用 fclose() 函数将其关闭非常重要。 fclose() 函数需要一个文件指针作为其参数,然后在关闭成功时返回 true ,或者在关闭失败时返回 false

After making a changes to the opened file it is important to close it with the fclose() function. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails.

Reading a File

一旦使用 fopen() 函数打开一个文件,就可以用称为 fread() 的函数读取它。此函数需要两个参数。它们必须是文件指针和文件长度(以字节表示)。

Once a file is opened using fopen() function it can be read with a function called fread(). This function requires two arguments. These must be the file pointer and the length of the file expressed in bytes.

可以使用 filesize() 函数找到文件长度,它以文件名作为其参数并返回文件的大小(以字节表示)。

The files length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.

所以,以下是使用 PHP 读取文件所需的步骤。

So here are the steps required to read a file with PHP.

  1. Open a file using fopen() function.

  2. Get the file’s length using filesize() function.

  3. Read the file’s content using fread() function.

  4. Close the file with fclose() function.

Example

以下示例将文本文件的内容分配给一个变量,然后在网页上显示该内容。

The following example assigns the content of a text file to a variable then displays those contents on the web page.

<html>
<head>
   <title>Reading a file using PHP</title>
</head>
<body>
   <?php
      $filename = "tmp.txt";
      $file = fopen( $filename, "r" );

      if( $file == false ) {
         echo ( "Error in opening file" );
         exit();
      }

      $filesize = filesize( $filename );
      $filetext = fread( $file, $filesize );
      fclose( $file );

      echo ( "File size : $filesize bytes" );
      echo ( "<pre>$filetext</pre>" );
   ?>
</body>
</html>

它将产生以下结果 −

It will produce the following result −

reading file

Writing a File

可以使用 PHP fwrite() 函数编写新文件或在现有文件中附加文本。该函数需要两个指定 file pointer 和要写入的数据字符串的参数。还可以选择包含第三个整数参数以指定要写入的数据的长度。如果包含第三个参数,则在达到指定长度后写入将停止。

A new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer and the string of data that is to be written. Optionally a third integer argument can be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached.

Example

以下示例创建一个新文本文件,然后在其中写一个简短的文本标题。关闭此文件后,使用 file_exist() 函数确认其存在,该函数将文件名作为参数

The following example creates a new text file then writes a short text heading inside it. After closing this file its existence is confirmed using file_exist() function which takes file name as an argument

<?php
   $filename = "/home/user/guest/newfile.txt";
   $file = fopen( $filename, "w" );

   if( $file == false ) {
      echo ( "Error in opening new file" );
      exit();
   }
   fwrite( $file, "This is  a simple test\n" );
   fclose( $file );
?>
<html>
<head>
   <title>Writing a file using PHP</title>
</head>
<body>
   <?php
      $filename = "newfile.txt";
      $file = fopen( $filename, "r" );

      if( $file == false ) {
         echo ( "Error in opening file" );
         exit();
      }

      $filesize = filesize( $filename );
      $filetext = fread( $file, $filesize );

      fclose( $file );

      echo ( "File size : $filesize bytes" );
      echo ( "$filetext" );
      echo("file name: $filename");
   ?>
</body>
</html>

它将产生以下结果 −

It will produce the following result −

writing file

我们已介绍了有关 PHP File System Function 章节中文件输入和输出的所有函数。

We have covered all the function related to file input and out in PHP File System Function chapter.