php fopen实现mysql错误日志记录
分享一篇关于php fopen实现mysql错误日志记录用法,因为数据库出错了我们肯定不能把错误日志保存到数据库了,所以我们利用了fopen来记录,实例代码如下:
<?php $time = date("Y-m-d H:i:s"); $message = $message . "rn$this->sql" . "rn客户IP:$ip" . "rn时间 :$time" . "rnrn"; $server_date = date("Y-m-d"); $filename = $server_date . ".txt"; $file_path = "error/" . $filename; $error_content = $message; //$error_content="错误的数据库,不可以链接"; $file = "error"; //设置文件保存目录 //建立文件夹 if (!file_exists($file)) { if (!mkdir($file, 0777)) { //默认的 mode 是 0777,意味着最大可能的访问权 die("upload files directory does not exist and creation failed"); } } //建立txt日期文件 if (!file_exists($file_path)) { //echo "建立日期文件"; fopen($file_path, "w+"); //首先要确定文件存在并且可写 if (is_writable($file_path)) { //使用添加模式打开$filename,文件指针将会在文件的开头 if (!$handle = fopen($file_path, 'a')) { echo "不能打开文件 $filename"; exit; } //将$somecontent写入到我们打开的文件中。 if (!fwrite($handle, $error_content)) { echo "不能写入到文件 $filename"; exit; } //echo "文件 $filename 写入成功"; echo "——错误记录被保存!"; //关闭文件 fclose($handle); } else { echo "文件 $filename 不可写"; } } else { //首先要确定文件存在并且可写 if (is_writable($file_path)) { //使用添加模式打开$filename,文件指针将会在文件的开头 if (!$handle = fopen($file_path, 'a')) { echo "不能打开文件 $filename"; exit; } //将$somecontent写入到我们打开的文件中。 if (!fwrite($handle, $error_content)) { echo "不能写入到文件 $filename"; exit; } //echo "文件 $filename 写入成功"; echo "——错误记录被保存!"; //关闭文件 fclose($handle); } else { echo "文件 $filename 不可写"; } } ?>
永久地址:http://www.phprm.com/develop/fs4124.html
转载随意~请带上教程地址吧^^