Php file opening, closing, writing judgment and execution code
Source: Internet
Author: User
Today, I learned about PHP file operation commands, including fopen, fwrite, fclose, and is_writable, but in use, in many cases, you may encounter various problems, such as file does not exist, no permission to write, opening failure, and writing failure. how to accurately control and judge the problems has become a "small problem" in PHP ", the following statements are extracted from the book.
The code is as follows:
$ Filename = "html/cache.txt ";
$ Contents = "I am Zhang Bin ";
If (is_writable ($ filename )){
If ($ handle = fopen ($ filename, "a") = false ){
Echo "failed to write file $ filename ";
Exit ();
}
If (fwrite ($ handle, $ contents) = false ){
Echo "failed to write file $ filename ";
Exit ();
}
Echo "the file $ filename is successfully written ";
Fclose ($ handle );
} Else {
Echo "file $ filename cannot be written ";
}
?>
Programming section: pay attention to the symbols of Chinese and English input methods, especially the ";" after Chinese!
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