dougai0138 2014-06-13 18:15
浏览 69
已采纳

无法写入文件

I would net your help concerning an issue I'm currently having with PHP

Here is the Code:

<?php

$ics_file   = 'schedule.ics';

if (is_writable($ics_file)) {
    if (!$handle = fopen($ics_file, 'w')) {
        echo "Cannot open file ($ics_file)

";
        exit;
    }

    # Write $ics_contents to opened file
    if (fwrite($handle, "foobar") === FALSE) {
        echo "Cannot write to file ($ics_file)

";
        exit;
    }

    # echo "Success, wrote to <b>schedule.ics</b><br>

";
    fclose($handle);
} else {
    echo "The file <b>$ics_file</b> is not writable

";
}
?>

On my Webpage I always receive "The file schedule.ics is not writable" so it can't access the File. Could you kindly give me a hint into the right direction?

  • 写回答

1条回答 默认 最新

  • duanlang1196 2014-06-13 19:10
    关注
    1. If file does not exists, is_writable causes your error. You can simply omit this condition and try to write to file and check the result of fopen:

      if ($handle = fopen($ics_file, 'w')) {
          if (fwrite($handle, $ics_contents) === FALSE)
              echo "Cannot write to file ($ics_file)
      
      ";
          else {
              # echo "Success, wrote to <b>schedule.ics</b><br>
      
      ";
          }
      
          fclose($handle);
          exit;
      }
      else {
          echo "Cannot write to file ($ics_file)
      
      ";
          exit;
      }
      
    2. If previous solution does not help, check directory/file permissions. File access depends on server configuration. Try to create file by your own, and set up permission to 777 (for testing). Then script should have access to write into it.

    3. You are reading whole file content to memory. In such case, if you don't need to have the file on disk, you can just send the content to the client using something like this PHP - send file to user.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码