Home > php教程 > php手册 > PHP文件打开、关闭、写入的判断与执行

PHP文件打开、关闭、写入的判断与执行

WBOY
Release: 2016-06-13 10:42:11
Original
1022 people have browsed it

今天学习PHP的文件操作命令,涉及了fopen、fwrite、fclose、is_writable,但在使用中,很多时候可能遇到文件不存在、没有权限写入、打开失败、写入失败等各种问题,如何准确的控制和判断成了PHP中的一个“小问题”,下面是从书上摘抄下来的语句。

  

  $filename = "html/cache.txt";
  $contents = "我是张斌";
  if(is_writable($filename)){
      if(($handle = fopen($filename,"a") )== false){
         echo "写入文件 $filename 失败";
         exit();
      }
     if(fwrite($handle,$contents) == false){
        echo "写入文件$filename失败";
        exit();
      }
     echo "写入文件 $filename 成功";

     fclose($handle);
   }else{
     echo "文件$filename不可写入";
   }
?>



编程小节:注意中英文输入法的符号,特别是中文后的“;”!
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template