php - Errno=9 错误的文件描述符在 php 中

标签 php file

我正在尝试使用以下代码在 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);

然后我得到:

Notice: fwrite(): write of 66 bytes failed with errno=9 Bad file descriptor in E:\xampp\htdocs\host\host\storyedit.php on line 20

Warning: fread(): Length parameter must be greater than 0 in E:\xampp\htdocs\host\host\storyedit.php on line 22

Notice: Undefined offset: 1 in E:\xampp\htdocs\host\host\storyedit.php on line 27

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

我试过:

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

没有效果。

最佳答案

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

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

关于fread中的error,try

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

关于php - Errno=9 错误的文件描述符在 php 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61568204/

相关文章:

javascript - 多次提交表单

file - 如何导入java.nio.file包

php - 具有绑定(bind)参数的 PDO sql 查询 If 更新中的语句

php - 如何在数据库中存储 session 变量

javascript - 使用 Javascript 上传进度(带或不带 XMLHttpRequest 2)

file - 如何从文件中删除最后 N 个字节

c# - 下载文件并自动保存到文件夹

javascript - (Javascript) 使用文件路径创建一个 File 对象

php - 想要显示ajax加载gif

php - PHP 与 JavaScript 的速度?