fopen()中文件名的PHP变量.文件名不能为空错误


PHP variable for file name in fopen(). File name can not be empty error

我有一个循环,显示php服务器中的每个文件作为链接。点击链接使用fopen打开文件,如下所示:

  <?php
  echo"<ol>";
  if ($handle = opendir('saves/')) {
      while (false !== ($entry = readdir($handle))) {
          if ($entry != "." && $entry != "..") {
              echo '<li style"display:block"><a href="#codeword">'.$entry.'</a></li><br>';

    }
}
closedir($handle);
  } 
  echo"</ol>";
  echo'<div id="codeword">';
  $file = $entry;
  $f = fopen($entry, "r") or exit("Unable to open file!");
  // read file line by line until the end of file (feof)
  while(!feof($f))
  {
    echo fgets($f)."<br />";
  }
  fclose($f);
  echo"</div>";

  ?>

但是当我运行它时,我得到了错误

fopen()函数。fopen]:文件名不能为空

我尝试回显$entry而不是fopen,它看起来像预期的那样。问题可能是文件的路径。我应该使用什么文件路径。提前感谢!

您可以在fopen() http://php.net/manual/en/function.fopen.php的php文档中看到,您的第一个参数是文件名。

也许你的值$entry是null或空。

在你发布的代码中,$entry将始终是false(如果它进入while)或undefined(如果它没有),因为while循环将永远不会结束,直到它是false