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