首页 > 解决方案 > PHP 读取文件 - 未完成

问题描述

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

<?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% 确定该文件存在。:) 我犯了什么基本错误?

标签: phpfile-read

解决方案


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

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


推荐阅读