Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the built-in function file_put_contents to write files and append content to PHP

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how PHP uses the built-in function file_put_contents to write files and append content, the article introduces in great detail, has a certain reference value, interested friends must read it!

The details are as follows:

Write content as an append

When the flags parameter value is set to FILE_APPEND, the new data is written by appending the contents of the existing file:

The behavior of file_put_contents () is actually the same as calling the fopen (), fwrite (), and fclose () functions in turn.

FILE_APPEND: write data as an append at the end of the file

Parameter description:

Parameters.

Description

The name of the file to which filename writes the data

The data to be written by the data. The type can be string,array (but not a multidimensional array), or a stream resource

Flags is optional, which specifies how to open / write files. Possible value: 1.FILE_USE_INCLUDE_PATH: check the built-in path of the filename copy

2.FILE_APPEND: write data as an append at the end of the file

3.LOCK_EX: locking files

Context is optional, and Context is a set of options through which you can modify text properties

The PHP built-in function file_put_contents is used to write to a file:

The simplest way to write the file_put_contents function is to take only two parameters, one is the file path and the other is the content to be written. The syntax is as follows:

File_put_contents (filepath,data)

If the file does not exist, the file_put_contents function automatically creates the file; if the file already exists, the original file is overwritten.

You can use the file_put_contents function to create and write a new file, or rewrite an existing file.

The following is an example of PHP code that uses the file_put_contents function:

The PHP code example creates a file with the path C:\ blabla\ filesys\ one.txt, whose content is one for all.

PHP built-in function file_put_contents is used to append content:

If you want to append content to an existing file, you can also use the file_put_contents function, just add an argument.

File_put_contents (filepath,data,flags)

When the value of flags is FILE_APPEND, it appends content to an existing file. That is, the third parameter, the flags implementation, appends the content to the end of the file, and if not, it will directly overwrite the previous data.

For example, if we want to append content to the C:\ blabla\ filesys\ one.txt file of the above example, we can write:

After executing the PHP file, we look at the C:\ blabla\ filesys\ one.txt file and find that the content of the file has been increased to:

One for all all for one

The file_put_contents function returns the number of bytes written to the file (number of bytes), or FALSE if there is an error.

The above is all the content of the article "how to use the built-in function file_put_contents to write files and append content to PHP". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report