首页 > 解决方案 > Errno=9 php 中的错误文件描述符

问题描述

我正在尝试使用以下代码在 php 中编写文件

    $filename = "uploads/stories/".$row['s_source'];
    $handle = fopen($filename, "r");
    if (filesize($filename) == 0) {
      $txt = "{====views====}{====likes====}{====chapters====}{====comments====}";
      fwrite($handle, $txt);
    }
    $content = fread($handle, filesize($filename));
    fclose($handle);

我得到:

注意:fwrite(): write of 66 bytes failed with errno=9 Bad file descriptor in E:\xampp\htdocs\host\host\storyedit.php 第 20 行

警告:fread():第 22 行 E:\xampp\htdocs\host\host\storyedit.php 中的长度参数必须大于 0

注意:未定义的偏移量:第 27 行 E:\xampp\htdocs\host\host\storyedit.php 中的 1

但是代码中没有错误(我认为)。有人可以帮助我吗?

我试过了:

  1. 删除 php.ini 中的 opcache.enable_cli=1
  2. 删除缓存

没有工作。

标签: phpfile

解决方案


您以读取权限打开,然后发出写入命令>您需要以读/写权限打开

$handle = fopen($filename, "r+");

关于 fread 中的错误,请尝试

if (filesize($filename) > 0) {
$content = fread($handle, filesize($filename));
}

推荐阅读