Home >Backend Development >PHP Tutorial >Judgment and execution of PHP file opening, closing, writing_PHP tutorial
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";
}
?>