从目录中读取所有文件内容 - php [英] Reading all file contents from a directory - php

查看:186
本文介绍了从目录中读取所有文件内容 - php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上是一个简单的工作
i想要显示位于指定文件夹中的所有文件的内容。

This is actually is an easy work i want to display contents of all files located in specified folder.

我传递目录名称

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

在第二页,

我是$($ entryname = readdir($ myDirectory))
{
if(is_dir($))

i am writing

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 />";
        }



. . .

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

but i am unable to read any file , i have changed their permissions too

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

i tried putting directory name statically and it works, any solution?

推荐答案

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

$entryname will contain JUST the filename, with no path information. You have to manually rebuild the path yourself. e.g.

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

如果没有读取文件代码中的显式路径,则尝试在脚本当前工作目录中打开并读取文件,不是您正在阅读文件名的导演。

Without the explicit path in your "read the file code", you're trying to open and read a file in the script's current working directory, not the director you're reading the filenames from.

这篇关于从目录中读取所有文件内容 - php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆