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

Har*_*850 4 php directory file

这实际上是一项简单的工作,我想显示位于指定文件夹中的所有文件的内容.

我正在传递目录名称

echo "<a href='see.php?qname=". $_name ."'>" . $row["qname"] . "</a>";
Run Code Online (Sandbox Code Playgroud)

在第二页,

我在写信

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 />";
        }
Run Code Online (Sandbox Code Playgroud)

...

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

我尝试静态放置目录名称,它的工作原理,任何解决方案?

Mar*_*c B 7

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

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

如果没有"读取文件代码"中的显式路径,那么您将尝试打开并读取脚本当前工作目录中的文件,而不是您正在读取文件名的控制器.

  • 我建议添加像`if($ file =='.'|| $ file =='..'){continue; 在Linux服务器上的时间之后 (2认同)

Abr*_*ver 5

更简单:

foreach(glob("$myDirectory/*") as $file) {
    foreach(file($file) as $line) {
        echo $line . "<br />";
    }
}
Run Code Online (Sandbox Code Playgroud)

更简单:

foreach(glob("$myDirectory/*") as $file) {
    echo nl2br(file_get_contents($file));
}
Run Code Online (Sandbox Code Playgroud)