Implementation of file_put_contents append and line feed in PHP, fileputcontents newline

Source: Internet
Author: User

Implementation of file_put_contents append and line feed in PHP, fileputcontents newline

In some PHP applications, you need to write logs or record some information. You can use fopen (), fwrite (), and fclose () operations. You can also use file_get_contents () and file_put_contents ().

File_put_contents () writes a file. By default, the file is rewritten, that is, the original content is replaced. The parameter FILE_APPEND is used for appending.

When the flags parameter value is set to FILE_APPEND, new data is written after the existing file content:

FILE_APPEND:Write data at the end of the file in append Mode

int file_put_contents ( string filename, string data [, int flags [, resource context]] )file_put_contents("log.txt", "Hello world everyone.", FILE_APPEND);

// Parameter description:

Filename // File Name of the data to be written

Data // The data to be written. The type can be string, array (but not multi-dimensional array), or stream resource.

Flags // (optional) specifies how to open/write files. Possible values:

FILE_USE_INCLUDE_PATH: // check the built-in path of the filename copy.

FILE_APPEND: // write data at the end of the file in append Mode

LOCK_EX: // lock the file

Context // optional. Context is a set of options that can be used to modify text attributes.

In many cases, line breaks are required to record logs. \ R \ n is not recommended because:

In windows, \ r \ n is a line feed.

In Mac, \ r is a line feed.

In Liunx, \ n is a line feed.

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

PHP_EOL

file_put_contents("log.txt", "Hello world everyone.".PHP_EOL, FILE_APPEND);

In the above PHP article, the implementation of file_put_contents append and line feed is all the content that I have shared with you. I hope to give you a reference, and I hope you can provide more support to the customer's house.

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.