How to implement file_put_contents Append and newline in PHP?

Source: Internet
Author: User
The following small series for everyone to bring a PHP file_put_contents append and line break implementation method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

In some applications of PHP, you need to write a log or record some information. You can do this using fopen (), fwrite (), and fclose (). You can also simply use file_get_contents () and file_put_contents ().

file_put_contents () write the file. The default is to re-write the file, which will replace the original content. Append the words using the parameter file_append.

Write content in append form when the flags parameter value is File_append, the new data is written in such a way that the content is appended after the contents of the existing file:

file_append: writes data appended to the end of the file

int file_put_contents (string filename, string data [, int flags [, resource context]]) file_put_contents ("Log.txt", "Hel Lo world Everyone. ", file_append);

Parameter description:

FileName//file name to write data to

Data//To be written. The type can be String,array (but not a multidimensional array), or a stream resource

Flags//optional, which specifies how to open/write files. Possible values:

file_use_include_path://Check the built-in path of the filename copy

file_append://writes data to the end of the file in append form

lock_ex://Lock the file

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

There are many times when logging requires a line break. It is not recommended to use \ r \ n because:

In Windows \ r \ n is a newline

In Mac \ r is a newline

In Liunx, \ n is a newline.

But PHP provides a constant to match different operating systems, namely:

Php_eol

File_put_contents ("Log.txt", "Hello World everyone.") Php_eol, File_append);

Above this PHP file_put_contents append and line-wrapping implementation method is small to share all the content of everyone, hope to give you a reference, but also hope that we support a lot of script home.

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.