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

Source: Internet
Author: User
This article mainly introduces PHP using built-in function file_put_contents write files and append content method, combined with instance form analysis File_put_contents function through the parameter set to write files and append content of the relevant skills, very simple and practical, A friend you need can refer to the following

This article explains how PHP uses built-in functions file_put_contents write files and append content. Share to everyone for your reference, as follows:

Write 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 to the contents of the existing file:

Copy the Code code as follows:

<?phpfile_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 () functions in turn.

File_append: Writes data appended to the end of the file

Parameter description:

Parameters

Description

FileName to write the data to
Data to be written to. The type can be String,array (but not a multidimensional array), or a stream resource
Flags are optional, which specifies how to open/write files. Possible values: 1.file_use_include_path: Check the built-in path of the filename copy
2.file_append: Writes data appended to the end of the file
3.LOCK_EX: Lock the file
Context is optional, and 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 a file_put_contents function is to use only two parameters, one for the file path and one for the content to be written, with the following syntax:

Copy the 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 that uses the File_put_contents function:


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

PHP built-in function file_put_contents for appending content:

If you want to append content to an existing file, you can also use the File_put_contents function, just add a parameter .

Copy the Code code as follows:

File_put_contents (Filepath,data,flags)

when the value of flags is file_append, it means that the content is appended to the existing file. That is: the third parameter, the flags implementation, appends the content to the file, and if no such parameter overwrites the previous data directly .

For example, we want to append content to the C:\blabla\filesys\one.txt file on the example above, 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 increased and became:

One for all and for one

The file_put_contents function returns the bytes written to the file (number of bytes), and returns FALSE if an error occurs.

Related Article

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.