This article illustrates how PHP uses built-in function file_put_contents to write files and append content. Share to everyone for your reference, specific as follows:
Writing to the content in append form
When the flags parameter value is set to File_append, the new data is written in such a way that the content is appended after the existing file content:
Copy Code code as follows:
<?php
File_put_contents ("Test.txt", "This is another something.", file_append);
?>
The behavior of file_put_contents () is actually equal to calling fopen (), fwrite (), and fclose () functionality in turn.
File_append: Write data appended to the end of the file
Parameter description:
Parameters
Description
FileName to be written to data by filename
Data to write to. The type can be String,array (but not a multidimensional array), or a stream resource
Flags are optional and specify how to open/write files. Possible values: 1.file_use_include_path: Check for the built-in path to the filename copy
2.file_append: Write data appended to the end of the file
3.LOCK_EX: Lock the file
Context optional, context is a set of options through which you can modify text properties
PHP built-in function file_put_contents for writing files:
The simplest way to write the File_put_contents function is to use only two parameters, one is the file path, the other is the content to be written, the syntax is as follows:
Copy Code code as follows:
File_put_contents (Filepath,data)
If the file does not exist, the File_put_contents function automatically creates the file, and if the file already exists, the original file is rewritten。
You can use the File_put_contents function to create and write a new file, or to rewrite an existing file .
Here is an example of a PHP code using the File_put_contents function:
The PHP code example creates a file with a path of C:\blabla\filesys\one.txt, the content of which is one for all.
PHP built-in function file_put_contents for appending:
If you want to append content to an existing file, you can also use the File_put_contents function, which requires only one parameter .
Copy Code code as follows:
File_put_contents (Filepath,data,flags)
when the value of flags is File_append, it indicates that the content is appended to the existing file. That is: the third parameter, the flags implementation, appends the content to the back of the file, without which the previous data will be overwritten directly。
For example, we want to append content to the C:\blabla\filesys\one.txt file above, so we can write:
After executing the PHP file, we looked at the C:\blabla\filesys\one.txt file and found that the contents of the file were increased and changed to:
One for all, for one
The file_put_contents function returns the number of bytes written to the file (count of bytes), and returns FALSE if an error occurs.
I hope this article will help you with the PHP program design.