Php 简明教程
PHP - Append File
在 PHP 中,fopen() 函数返回一个文件指针,用于不同的打开模式,例如“w”表示写入模式,“r”表示读取模式,“r+”或“r+”表示同时读/写操作,以及“a”表示追加模式。
In PHP, the fopen() function returns the file pointer of a file used in different opening modes such as "w" for write mode, "r" read mode and "r+" or "r+" mode for simultaneous read/write operation, and "a" mode that stands for append mode.
当使用“w”模式参数打开文件时,它总是打开一个新文件。这意味着如果文件已经存在,其内容将丢失。随后的 fwrite() 函数将数据放在文件开始位置。
When a file is opened with "w" mode parameter, it always opens a new file. It means that if the file already exists, its content will be lost. The subsequent fwrite() function will put the data at the starting position of the file.
假设存在一个名为 “new.txt” 的文件,内容如下:
Assuming that a file "new.txt" is present with the following contents −
Hello World
TutorialsPoint
PHP Tutorial
以下语句 -
The following statement −
$fp = fopen("new.txt", "w");
删除所有现有数据,然后再写入新内容。
Erases all the existing data before new contents are written.
Read/Write Mode
显然,在以“r”模式打开文件时无法添加新数据。然而, “r+”或“w+” mod 在“r/w”模式下打开文件,但在打开文件后一个 fwrite() 语句仍会覆盖内容。
Obviously, it is not possible to add new data if the file is opened with "r" mode. However, "r+" or "w+" mod opens the file in "r/w" mode, but still a fwrite() statement immediately after opening a file will overwrite the contents.
Example
查看以下代码:
Take a look at the following code −
<?php
$fp = fopen("new.txt", "r+");
fwrite($fp, "PHP-MySQL Tutorial\n");
fclose($fp);
?>
利用这段代码, “new.txt” 文件的内容现在变为 −
With this code, the contents of the "new.txt" file will now become −
PHP-MySQL Tutorial
lsPoint
PHP Tutorial
为了确保新内容添加到现有文件的末尾,我们需要在写操作前手动将文件指针放置到末尾。(文件的初始指针位置在第 0 个字节)
To ensure that the new content is added at the end of the existing file, we need to manually put the file pointer to the end, before write operation. (The initial file pointer position is at the 0th byte)
The fseek() Function
PHP 的 fseek() 函数让您能够将文件指针放置到任何您想要的位置 −
PHP’s fseek() function makes it possible to place the file pointer anywhere you want −
fseek(resource $stream, int $offset, int $whence = SEEK_SET): int
$whence 参数是指从哪里计算偏移量。它的值有 −
The $whence parameter is from where the offset is counted. Its values are −
-
SEEK_SET − Set position equal to offset bytes.
-
SEEK_CUR − Set position to current location plus offset.
-
SEEK_END − Set position to end-of-file plus offset.
Example
因此,我们需要利用 fseek() 函数将指针移动到末尾,如此以下代码,将新内容添加到末尾。
So, we need to move the pointer to the end with the fseek() function as in the following code which adds the new content to the end.
<?php
$fp = fopen("new.txt", "r+");
fseek($fp, 0, SEEK_END);
fwrite($fp, "\nPHP-MySQL Tutorial\n");
fclose($fp);
?>
现在检查 “new.txt” 的内容。它将具有以下文本 −
Now check the contents of "new.txt". It will have the following text −
Hello World
TutorialsPoint
PHP Tutorial
PHP-MySQL Tutorial
Append Mode
无需手动将指针移动到末尾,fopen() 函数中的“a”参数会以追加模式打开文件。每个 fwrite() 语句将内容添加到现有内容的末尾,通过自动将指针移动到 SEEK_END 位置。
Instead of manually moving the pointer to the end, the "a" parameter in fopen() function opens the file in append mode. Each fwrite() statement adds the content at the end of the existing contents, by automatically moving the pointer to SEEK_END position.
<?php
$fp = fopen("new.txt", "a");
fwrite($fp, "\nPHP-MySQL Tutorial\n");
fclose($fp);
?>
fopen() 函数允许的模式之一是“r+”模式,使用此模式,文件执行读/追加操作。要读取任何位置的数据,您可以利用 fseek() 将指针置于所需的字节。但是,每次 fwrite() 操作仅在末尾写新内容。
One of the allowed modes for fopen() function is "r+" mode, with which the file performs read/append operation. To read data from any position, you can place the pointer to the desired byte by fseek(). But, every fwrite() operation writes new content at the end only.
Example
在下面的程序中,文件在“a+”模式下打开。为了读取第一行,我们使文件位置偏移 0——从开头开始。然而, fwrite() 语句仍然在末尾添加新内容,并且不会覆盖后面的一行,就像在“r+”模式打开时那样。
In the program below, the file is opened in "a+" mode. To read the first line, we shift the file position to 0the position from beginning. However, the fwrite() statement still adds new content to the end and doesn’t overwrite the following line as it would have if the opening mode "r+" mode.
<?php
$fp = fopen("new.txt", "a+");
fseek($fp, 0, SEEK_SET);
$data = fread($fp, 12);
echo $data;
fwrite($fp, "PHP-File Handling");
fclose ($fp);
?>
因此,如果文件在“r+/w+”模式或“a/a+”模式下打开,我们就可以追加数据到现有文件。
Thus, we can append data to an existing file if it is opened in "r+/w+" mode or "a/a+" mode