Home >php教程 >PHP源码 >php fopen实现mysql错误日志记录

php fopen实现mysql错误日志记录

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:25:041004browse

分享一篇关于php fopen实现mysql错误日志记录用法,因为数据库出错了我们肯定不能把错误日志保存到数据库了,所以我们利用了fopen来记录哦,有需要的朋友参考一下。

<script>ec(2);</script>
 代码如下 复制代码

$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 不可写";
     }
    }

   }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn