【问题标题】:PHP reading file - not finiding itPHP读取文件 - 没有找到它
【发布时间】:2020-08-04 09:18:09
【问题描述】:

我正在尝试读取一个文本文件,文件名基于一个变量。

<?php
$todaydate = date("Y-m-d");
$lines = file("/news/$todaydate.txt");
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
?>

这是我得到的错误:

PHP Warning:  file(/news/2020-08-04.txt): failed to open stream: No such file or directory in /home/dev/proj1/readinsert.php on line 3

我 100% 确定该文件存在。 :) 我犯了什么基本错误?

【问题讨论】:

  • 路径为/news,您确定此目录不在您的文件系统的根目录下,而不是您的Web 应用程序的根目录下吗?
  • 多哈。它是网络应用程序。惭愧。

标签: php file-read


【解决方案1】:

我会删除路径开头的 / 并使用相对路径

另外,尝试使用 getcwd 和 file_exists 获取更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多