dounao5856 2010-04-27 06:51
浏览 104

无法打开文件进行写入

I am trying to write to a file. I do a file_exists check on it before I do fopen and it returns true (the file does exist).

However, the file fails this code and gives me the error every time:

$handle = fopen($filename, 'w');
if($handle)
{
    flock($handle, LOCK_EX);
    fwrite($handle, $contents);
}
else
{
    echo 'ERROR: Unable to open the file for writing.',PHP_EOL;
    exit();
}
flock($handle, LOCK_UN);
fclose($handle);

Is there a way I can get more specific error details as to why this file does not let me open it for writing? I know that the filename is legit, but for some reason it just wont let me write to it. I do have write permissions, I was able to write and write over another file.

  • 写回答

4条回答 默认 最新

  • douliexing2195 2010-04-27 06:54
    关注

    If you're using php 5.2+ you might be interested in error_get_last().
    On your development system you can also increase the error reporting level, either within the script via error_reporting() or (preferably) in your php.ini.

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    $handle = fopen($filename, 'w');
    if(!$handle) {
      echo 'ERROR: Unable to open the file for writing.',PHP_EOL;
      var_dump(error_get_last());
      exit();
    }
    
    flock($handle, LOCK_EX);
    fwrite($handle, $contents);
    flock($handle, LOCK_UN);
    fclose($handle);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么