PHP implementation Logging (file) phpphp logging
In the project, it is often necessary to record some operational information, or to print some key variables, or to import Excel files and withdraw records. Often encounter, encapsulate a method, have a bad place or supplement please leave a message.
header(‘Content-Type: text/html; charset=UTF-8‘); #记录文件 $dir 文件目录;$filename 文件名;$data 存贮数据;$Separator 分隔符 function RecordsFile( $dir,$filename,$data=array(),$Separator="," ){ if( !$dir || !$filename ||!is_array($data) ) return false; if(!is_dir($dir)) mkdir($dir); $content = implode( $Separator,$data ); $result = file_put_contents( $dir.‘/‘.$filename,(date(‘Y-m-d h:i:s‘,time())).‘ ‘.$content."\r\n",FILE_APPEND | LOCK_EX ); return $result; }
The test is as follows:
$dir = ‘E:\website\test\upload/user_excel‘; //$dir = ‘./upload/user_excel‘;//本地测试报错,权限不足,跟系统环境有关;相关解决可谷歌 $filename = date(‘Ymd‘).‘.txt‘; $data = array(‘sun‘,24,"是鸟是鱼是尘埃!"); $Separator = "=>"; RecordsFile( $dir,$filename,$data,$Separator );
Tian Jian.
PHP implementation logging (file)