php fopen實現mysql錯誤記錄檔記錄

來源:互聯網
上載者:User
 代碼如下 複製代碼

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

   }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.