如何在PHP中列出目录、子目录和所有文件

3
我想能够列出"./"文件夹中的所有目录、子目录和文件,即名为fileSystem的项目文件夹,其中包含这个php文件scanDir.php。
您可以在这里查看我拥有的目录结构: enter image description here 目前,它只会返回mkdir目录根目录中的子目录文件,但不会返回该子目录中的任何文件夹。
如何修改代码以便展示fileSystem文件夹中的所有文件、目录、子目录及其文件和子目录,假设运行的php文件名为scanDir.php,其代码如下。

 $path = "./";

 if(is_dir($path))

{
    $dir_handle = opendir($path);

    //extra check to see if it's a directory handle.
    //loop round one directory and read all it's content.
    //readdir takes optional parameter of directory handle.
    //if you only scan one single directory then no need to passs in argument.
    //if you are then going to scan into sub-directories the argument needs 
    //to be passed into readdir.
    while (($dir = readdir($dir_handle))!== false) 
    {
    if(is_dir($dir))
    {
    echo "is dir: " . $dir . "<br>"; 


    if($dir == "mkdir") 
        {
        $sub_dir_handle = opendir($dir);
        while(($sub_dir = readdir($sub_dir_handle))!== false)
            {
            echo "--> --> contents=$sub_dir <br>";
            }
    }



    }    
        elseif(is_file($dir)) 
         {
            echo "is file: " . $dir . "<br>"  ;
        }
    }
closedir($dir_handle); //will close the automatically open dir.
}

else {

    echo "is not a directory";
}


使用 scandir 列出所有文件夹和文件,然后使用 is_file 判断该项是文件还是文件夹,并重复此过程。我很快会回答完整的代码。 - undefined
谢谢,我期待看到你的完整代码。 - undefined
2个回答

5

使用scandir函数查看目录中所有的内容,并使用is_file函数检查项目是否为文件或下一个目录,如果是目录,则重复相同的操作。

因此,这是全新的代码。

function listIt($path) {
$items = scandir($path);

foreach($items as $item) {

    // Ignore the . and .. folders
    if($item != "." AND $item != "..") {
        if (is_file($path . $item)) {
            // this is the file
            echo "-> " . $item . "<br>";
        } else {
            // this is the directory

            // do the list it again!
            echo "---> " . $item;
            echo "<div style='padding-left: 10px'>";
            listIt($path . $item . "/");
            echo "</div>";
        }
    }
  }
}

echo "<div style='padding-left: 10px'>";
listIt("/");
echo "</div>";

您可以在我的Web服务器上查看实时演示,顺便说一句,我只会保留此链接一会儿

当您看到“->”时,那是文件,“-->”是目录

无HTML的代码:

function listIt($path) {
$items = scandir($path);

foreach($items as $item) {
    // Ignore the . and .. folders
    if($item != "." AND $item != "..") {
        if (is_file($path . $item)) {
            // this is the file
            // Code for file
        } else {
            // this is the directory
            // do the list it again!
            // Code for directory
            listIt($path . $item . "/");
        }
    }
  }
}

listIt("/");

演示可能需要一段时间才能加载完毕,因为它包含了大量的项目。


谢谢。如果我想显示一个返回上一级目录或进入子目录的方法,我需要从if语句中删除"."和".."的代码吗?还是返回上一级或进入子目录、隐藏和显示目录是JavaScript的专长? - undefined
我正在我的Mac上运行代码,并且它在我的Mac上显示所有内容,我该如何限制它只显示scandir1.php中的文件目录? - undefined
让我们在聊天中继续这个讨论:点击此处进入聊天室 - undefined
谢谢,我已经将更改限制在用户文件夹上(如图所示),但是我看不到其中的任何文件,我该如何操作? - undefined
我已经开始在聊天讨论室中发送消息。 - undefined
显示剩余5条评论

4

PHP有一些强大的内置函数用于查找文件和文件夹,个人喜欢recursiveIterator类系列。

$startfolder=$_SERVER['DOCUMENT_ROOT'];
$files=array();


foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $startfolder, RecursiveDirectoryIterator::KEY_AS_PATHNAME ), RecursiveIteratorIterator::CHILD_FIRST ) as $file => $info ) {
    if( $info->isFile() && $info->isReadable() ){
        $files[]=array('filename'=>$info->getFilename(),'path'=>realpath( $info->getPathname() ) );
    }
}

echo '<pre>',print_r($files,true),'</pre>';

我应该在当前代码的哪里添加这个?还是需要在一个单独的文件中创建一个新的集合? - undefined
有没有一种方法可以在修改现有代码的同时完成这个任务? - undefined
哦,而且是否可以从文件系统文件夹而不是服务器根目录进行操作?也就是说,循环遍历文件系统文件夹的子目录和目录,而不是['DOCUMENT_ROOT']。我该如何做到这一点? - undefined
是的,这将在文件系统上工作,而不仅仅在文档根目录中。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接