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

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.