Home > Daily Programming > PHP Knowledge > How to write data to a specified file in PHP? (Pictures + Videos)

How to write data to a specified file in PHP? (Pictures + Videos)

藏色散人
Release: 2018-09-30 09:24:07
Original
6816 people have browsed it

PHP writes data to a file, which is one of the common operations of PHP file processing. PHP file processing includes file writing, reading, closing and other operations. This article will give you a detailed introduction to the two methods of writing files in PHP. Hope it helps those in need!

First create an empty txt file. Here we create an empty file of 1.txt.

The first method: fwrite function

<?php
$file=fopen(&#39;1.txt&#39;,&#39;rb+&#39;);
var_dump(fwrite($file,&#39;php中文网&#39;));
Copy after login

Use fopen to open the 1.txt file, and set the opening mode to rb. (rb means opening a binary file for reading and writing, and only allows reading and writing data.)

Then write the data of "PHP Chinese Network" into $file through the fwrite function, here the fwrite function The first parameter indicates the file to be written to, and the second parameter indicates the data content to be written. Then we use var_dump to print out the result

int(12)
Copy after login

, which means that the written content is an integer of 12 characters.

Note, the number of bytes occupied by a Chinese character is related to the encoding:

UTF8: One Chinese character = 3 bytes

GBK: One Chinese character = 2 bytes

At this point we have successfully written the data into the txt file using PHP.

If we want to close the file, we can use the fclose function to perform the closing operation:

fclose($file);
Copy after login

After the file is closed, the file cannot be written.

Second method: file_put_contents function

file_put_contents(&#39;1.txt&#39;,&#39;html中文网&#39;);
Copy after login

Here we write the "html Chinese website" field into the txt file through the file_put_contents function .

Note, when we use this function to write to a file, its content will overwrite the existing file content.

If we want the existing content not to be cleared, we can splice the existing content with the content to be written.

The sample code is as follows:

<?php
$file=fopen(&#39;1.txt&#39;,&#39;rb+&#39;);
$data=fread($file.filesize(&#39;1.txt&#39;));
file_put_contents(&#39;1.txt&#39;,"$data.html中文网");
Copy after login

As shown in this code, first open the existing files through the fopen and fread functions to obtain the content and splice them.

Then after finally writing the file, the effect is as follows:

How to write data to a specified file in PHP? (Pictures + Videos)

This article is about PHP writing to the fileThat is, PHP writes data to files Detailed explanation of the two methods. 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 write data to a specified 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