"PHP write log" php writes log to file

Source: Internet
Author: User
Tags php write

PHP writes content to file, writes logs to log file

<?PHPHeader("content-type:text/html; Charset=utf-8 ");/********************1, write content to file, append content to file 2, open and read file contents ********************/ $file= ' Log.txt ';//the file name to write to the file (which can be any file name), if the file does not exist, creates a $content= "Content written for the first time \ n"; if($f=file_put_contents($file,$content, File_append)) {//This function supports version (PHP 5)  Echo"Write succeeded. <br/> "; } $content= "Content to be written second time"; if($f=file_put_contents($file,$content, File_append)) {//This function supports version (PHP 5)     Echo"Write succeeded. <br/> "; } if($data=file_get_contents($file)){;//This function supports version (PHP 4 >= 4.3.0, PHP 5)  EchoThe contents of the write file are:$data"; }?>

Note: log.txt This file Web server user must have read and Write permissions (Nginx is nginx user, Apache under the default user is daemon), generally need to set this file to 777 properties.

PHP file_put_contents Append Data
int file_put_contents (string filename, string data [, int flags [, resource context]])
and sequentially call fopen (), fwrite (), and the fclose () function.
filename to which to write the file.
Data to be written to. The type can be String,array (but cannot be a multidimensional array) or a stream resource.
The flags flags can be file_use_include_path,file_append and/or LOCK_EX (get an exclusive lock) and be especially cautious when using File_use_include_path.
Context a context resource.
The function returns the number of bytes written to the data in the file.
File_put_contents ("Rote.txt", "CC", file_append);
? The third parameter implementation appends the contents to the file, and if not, overwrites the previous data directly.

"PHP write log" php writes log to file

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.