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

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:

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);

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
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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools