[phpstudy]php fopen实现mysql错误日志记录

更新时间:2017-05-09    来源:mysql教程    手机版     字体:

【www.bbyears.com--mysql教程】

 代码如下

$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.bbyears.com/shujuku/32517.html

猜你感兴趣

热门标签

更多>>

本类排行