php - 从目录中读取所有文件内容 - php

标签 php file directory

这其实很简单

我想显示位于指定文件夹中的所有文件的内容。

我正在传递目录名

echo "<a href='see.php?qname=". $_name ."'>" . $row["qname"] . "</a>";

在第二页,

我正在遍历目录内容

while($entryname = readdir($myDirectory)) 
{
            if(is_dir($entryname))
            {
                continue;
            }   
            if($entryname=="." || $entryname==".." )
            {}
            else
            {
                if(!is_dir($entryname))
                {   
                    $fileHandle=fopen($entryname, "r");
                    
                    while (!feof($fileHandle) ) {
           $line = fgets($fileHandle);
           echo $line . "<br />";
        }

. . .

但是我无法读取任何文件,我也更改了它们的权限。

我尝试静态放置目录名称,但效果很好, 有人可以建议我做错了什么吗?

最佳答案

$entryname 将只包含文件名,没有路径信息。您必须自己手动重建路径。例如

$dh = opendir('/path/you/want/to/read/');
while($file = readdir($dh)) {
    $contents = file_get_contents('/path/you/want/to/read/' . $file);
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^---include path here
}

如果您的“读取文件代码”中没有明确的路径,您将尝试打开并读取脚本当前工作目录中的文件,而不是您正在从中读取文件名的目录。

关于php - 从目录中读取所有文件内容 - php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19982692/

相关文章:

php - 跟踪非现场点击

c++ - 将 QFile 转换为文件*

c++ - 递归走目录问题

android - 4K 的资源限定符(不是 -xxxxhdpi?)807dpi

php - 比较mysql中的字符串

javascript - Telegram API 使用 php 或 javascript 发送消息?

c - 字符串和文件

Mysql命令行恢复错误 "The system cannot find the file specified."

php - 将事件添加到 Google 日历时的时差

c# - 如何将内存中的位图保存到 ZipArchive,而不先将位图保存到文件系统?