This article describes how PHP uses the built-in function file_put_contents to write files and append content, and analyzes the techniques related to file_put_contents functions using parameter settings to write and append files, it is very simple and practical. if you need it, you can refer to the example in this article to describe how PHP uses the built-in function file_put_contents to write files and append content. We will share this with you for your reference. The details are as follows:
PHP built-in function file_put_contents is used to write files:
The file_put_contents function is the simplest method. it can use only two parameters, one is the file path and the other is the content to be written. The syntax is as follows:
The code is as follows:
File_put_contents (filepath, data)
The code is as follows:
File_put_contents (filepath, data, flags)
When the flags value is FILE_APPEND, it indicates that the content is appended to an existing file. That is, the third flags parameter adds content to the end of the file. If this parameter is not provided, the previous data will be overwritten..
For example, to append content to the C: \ blabla \ filesys \ one.txt file in the preceding example, we can write as follows:
<?php$path ="C:\\blabla\\filesys\\one.txt";$content = " all for one";file_put_contents($path,$content,FILE_APPEND);if (file_exists($path)) {echo "ok";}else {echo "ng";}?>
After executing the PHP file, let's look at the C: \ blabla \ filesys \ one.txt file again and find that the file content has increased:
One for all for one
The file_put_contents function returns the number of bytes written to the file (number of bytes). If an error occurs, FALSE is returned.
I hope this article will help you with PHP programming.