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.