Home > Article > Daily Programming > How to append content to the end of the file in PHP? (Pictures + Videos)
This article mainly introduces the two methods of appending content to the end of the file in PHP.
In previous articles, we have introduced you to some basic operations such as writing and reading PHP files. Friends who have learned the relevant knowledge points of PHP file processing will be easier to understand and master the operation of appending content to the end of the file in PHP. In fact, this knowledge point is very simple.
Below we will introduce to you the method of php append writing to file through simple code:
The first method: fwrite
<?php $file = fopen('1.txt','ab+'); echo fwrite($file,'韦小宝!');
As shown in the above code, we use the fwrite function to add the field content of "Wei Xiaobao" to the end of the file 1.txt.
Note: The fwrite() function is used to write files.
The original content of our 1.txt file here is:
html中文网.PHP中文网
The result of the operation in the above code is:
As you can see from the picture, we added 12 characters to 1.txt, which is the content appended above. The content of the final file is as shown in the picture above.
Note: When the fopen opening mode is ab, it means opening a binary file for reading and writing, allowing reading or appending data at the end of the file.
Second method: file_put_contents
<?php file_put_contents('1.txt', '灭绝', FILE_APPEND);
PHP adds content to the end of the file. Just master the file_put_contents method in PHP.
file_put_contents Indicates writing a string to a file. The second parameter is the content to be added.
The final result is the same as calling fopen(), fwrite() and fclose() in sequence. The content of the file after appending is as follows:PHP video tutorial on the PHP Chinese website! Welcome everyone to refer to and study!
The above is the detailed content of How to append content to the end of the file in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!