Home >Backend Development >PHP Tutorial >Judgment and execution of PHP file opening, closing, writing_PHP tutorial

Judgment and execution of PHP file opening, closing, writing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:43:42833browse

Today I learned the file operation commands of PHP, including fopen, fwrite, fclose, and is_writable. However, in use, many times you may encounter files that do not exist, do not have permission to write, open fails, write How to accurately control and judge various problems such as entry failure has become a "small problem" in PHP. The following is a sentence excerpted from the book.

$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 "Write to file $filename successful";

fclose($handle);
}else{
echo "The file $filename cannot be written";
}
?>

Judgment and execution of PHP file opening, closing, writing_PHP tutorial

Programming section: Pay attention to the symbols of Chinese and English input methods, especially the ";" after Chinese!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478825.htmlTechArticleToday I learned the file operation commands of PHP, involving fopen, fwrite, fclose, is_writable, but in use, many Sometimes you may encounter files that do not exist, do not have permission to write, fail to open,...
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