Php 简明教程
PHP - Write File
PHP 内置函数库提供了两个函数来执行文件流的写操作。这些函数是 fwrite() 和 fputs() 。
PHP’s built-in function library provides two functions to perform write operations on a file stream. These functions are fwrite() and fputs().
为了能在文件中写入数据,必须以写模式 (w)、追加模式 (a)、读/写模式 (r+ 或 w+) 或二进制写/追加模式 (rb+、wb+ 或 wa) 打开此文件。
To be able to write data in a file, it must be opened in write mode (w), append mode (a), read/write mode (r+ or w+) or binary write/append mode (rb+, wb+ or wa).
The fputs() Function
fputs() 函数将字符串写入以可写模式打开的文件中。
The fputs() function writes a string into the file opened in a writable mode.
fputs(resource $stream, string $string, int $length)
此处, $stream 参数是对以可写模式打开的文件句柄。 $string 参数是要写入的数据,而 $length 是可选参数,指定要写入的最大字节数。
Here, the $stream parameter is a handle to a file opened in a writable mode. The $string parameter is the data to be written, and $length is an optional parameter that specifies the maximum number of bytes to be written.
fputs() 函数返回写入的字节数,或 false (如果函数执行失败)。
The fputs() function returns the number of bytes written, or false if the function is unsuccessful.
Example
以下代码打开了新文件,其中写入了一个字符串,并返回写入的字节数。
The following code opens a new file, writes a string in it, and returns the number of bytes written.
<?php
$fp = fopen("hello.txt", "w");
$bytes = fputs($fp, "Hello World\n");
echo "bytes written: $bytes";
fclose($fp);
?>
它将生成以下 output −
It will produce the following output −
bytes written: 12
Example
如果需要在较早存在的文件中添加文本,必须以追加模式打开此文件( a )。让我们在前面的示例中再向同一文件中添加一个字符串。
If you need to add text in an earlier existing file, it must be opened in append mode (a). Let us add one more string in the same file in previous example.
<?php
$fp = fopen("hello.txt", "a");
$bytes = fputs($fp, "Hello PHP");
echo "bytes written: $bytes";
fclose($fp);
?>
如果你在文本编辑器中打开“hello.txt”文件,你应该可以看到其中的这两行。
If you open the "hello.txt" file in a text editor, you should see both the lines in it.
Example
在以下 PHP 脚本中,一个已存在的文件 (hello.txt) 会在循环中逐行读取,并且每行会被写入到另一个文件 (new.txt)
In the following PHP script, an already existing file (hello.txt) is read line by line in a loop, and each line is written to another file (new.txt)
假设“hello.txt”包含以下文本:
It is assumed thar "hello.txt" consists of following text −
Hello World
TutorialsPoint
PHP Tutorials
以下是创建现有文件副本的 PHP 代码:
Here is the PHP code to create a copy of an existing file −
<?php
$file = fopen("hello.txt", "r");
$newfile = fopen("new.txt", "w");
while(! feof($file)) {
$str = fgets($file);
fputs($newfile, $str);
}
fclose($file);
fclose($newfile);
?>
新创建的“new.txt”文件应具有完全相同的内容。
The newly created "new.txt" file should have exactly the same contents.
The fwrite() Function
frwrite() 函数是 fread() 函数的对应函数。它执行二进制安全写操作。
The frwrite() function is a counterpart of fread() function. It performs binary-safe write operations.
fwrite(resource $stream, string $data, ?int $length = null): int|false
此处, $stream 参数是指向以可写模式打开的文件的资源。要写入该文件的数据提供在 $data 参数中。可以提供可选的 $length 参数来指定要写入的字节数。其应该是 int ,在写入长度字节之后或到达数据的末尾(以先发生的为准)后,写操作会停止。
Here, the $stream parameter is a resource pointing to the file opened in a writable mode. Data to be written to the file is provided in the $data parameter. The optional $length parameter may be provided to specify the number of bytes to be written. It should be int, writing will stop after length bytes have been written or the end of data is reached, whichever comes first.
fwrite() 函数返回写入的字节数,或 false (如果失败,则会同时返回 E_WARNING)。
The fwrite() function returns the number of bytes written, or false on failure along with E_WARNING.
Example
以下程序打开新文件,执行写操作并显示写入的字节数。
The following program opens a new file, performs write operation and displays the number of bytes written.
<?php
$file = fopen("/PhpProject/sample.txt", "w");
echo fwrite($file, "Hello Tutorialspoint!!!!!");
fclose($file);
?>
Example
在下面给出的示例代码中,一个现有文件“welcome.png”是以二进制读模式打开的。fread() 函数用于将它的字节读入“$data”变量中,然后将其写入到另一个文件“new.png”:
In the example code given below, an existing file "welcome.png" in opened in binary read mode. The fread() function is used to read its bytes in "$data" variable, and in turn written to another file "new.png" −
<?php
$name = "welcome.png";
$file = fopen($name, "rb");
$newfile = fopen("new.png", "wb");
$size = filesize($name);
$data = fread($file, $size);
fwrite($newfile, $data, $size);
fclose($file);
fclose($newfile);
?>
运行以上代码。当前目录中现在应有一个现有“welcome.png”文件的副本。
Run the above code. The current directory should now have a copy of the existing "welcome.png" file.