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)

Sep 19, 2018 pm 04:51 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)