Home > Daily Programming > PHP Knowledge > How to append content to the end of the file in PHP? (Pictures + Videos)

How to append content to the end of the file in PHP? (Pictures + Videos)

藏色散人
Release: 2018-09-19 16:51:50
Original
8648 people have browsed it

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(&#39;1.txt&#39;,&#39;ab+&#39;);
echo fwrite($file,&#39;韦小宝!&#39;);
Copy after login

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中文网
Copy after login

The result of the operation in the above code is:

How to append content to the end of the file in PHP? (Pictures + Videos)

How to append content to the end of the file in PHP? (Pictures + Videos)

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(&#39;1.txt&#39;, &#39;灭绝&#39;, FILE_APPEND);
Copy after login

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:


How to append content to the end of the file in PHP? (Pictures + Videos)

This article is about two methods of adding content to the end of the file in PHP. Isn’t it very What about simplicity? Hope it helps friends who need it!

If you want to learn more about PHP, you can follow the

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template