PHP fopen to implement MySQL error logging

Source: Internet
Author: User
The code is as follows Copy Code

$time = Date ("y-m-d h:i:s");
$message = $message. "Rn$this->sql". "RN Client IP: $ip". "RN Time: $time". "Rnrn";

$server _date = Date ("y-m-d");
$filename = $server _date. ". txt";
$file _path = "error/". $filename;
$error _content = $message;
$error _content= "Wrong database, can not link";
$file = "Error"; Set File Save Directory

Create a folder
if (!file_exists ($file)) {
if (!mkdir ($file, 0777)) {
The default mode is 0777, which means the maximum possible access rights
Die ("Upload files directory does not exist and creation failed");
}
}

Set up txt date file
if (!file_exists ($file _path)) {

echo "Build date File";
fopen ($file _path, "w+");

First, make sure that the file exists and can be written
if (is_writable ($file _path)) {
Open $filename with Add mode, and the file pointer will be at the beginning of the file
if (! $handle = fopen ($file _path, ' a ')) {
echo "Cannot open file $filename";
Exit
}

Write the $somecontent to the file we opened.
if (!fwrite ($handle, $error _content)) {
echo "cannot be written to the file $filename";
Exit
}

echo "File $filename write Success";

echo "--Error Record saved!";

Close File
Fclose ($handle);
} else {
echo "File $filename not writable";
}

} else {
First, make sure that the file exists and can be written
if (is_writable ($file _path)) {
Open $filename with Add mode, and the file pointer will be at the beginning of the file
if (! $handle = fopen ($file _path, ' a ')) {
echo "Cannot open file $filename";
Exit
}

Write the $somecontent to the file we opened.
if (!fwrite ($handle, $error _content)) {
echo "cannot be written to the file $filename";
Exit
}

echo "File $filename write Success";
echo "--Error Record saved!";

Close File
Fclose ($handle);
} else {
echo "File $filename not writable";
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.