【问题标题】:How to list directories, sub-directories and all files in php如何在php中列出目录、子目录和所有文件
【发布时间】:2016-01-16 06:51:14
【问题描述】:

我希望能够列出“./”文件夹中的所有目录、子目录和文件,即名为 fileSystem 的项目文件夹,其中包含这个 php 文件 scanDir.php。

您可以查看我在这里获得的目录系统:

此时它只会返回 mkdir 目录根目录中的子目录文件夹/文件,但不会返回该子目录中的任何文件夹。

如果正在运行的 php 文件名为 scanDir.php 并且下面提供了代码,我该如何修改代码,以便它演示 fileSystem 文件夹中的所有文件、目录、子目录及其文件和子目录。 这是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项目是文件,否则是文件夹并重复该过程。我很快就会用完整的代码来回答
  • 谢谢,期待看到你的完整代码

标签: php file directory subdirectory


【解决方案1】:

使用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>";

You can see the live demo here in my webserver, btw, I will keep this link just for a second

当你看到“->”时,它是一个文件,而“-->”是一个目录

没有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 保留的目录?
  • 好吧,如果你注意到了,所有的目录都在div 块中,所以只需通过“id”来识别它们并用于示例jQuery 函数.hide().show()跨度>
  • 代码末尾的那一行是什么意思 listIt("/");做什么?
  • 我在我的 mac 上运行代码,它显示了我的 mac 上的所有内容,我如何限制它使其只显示 scandir1.php 中的文件目录
  • 是的,您必须设置完整路径,即“/”,只需在此处从根目录输入您的路径 :)
【解决方案2】:

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>';

【讨论】:

  • 我应该在当前代码的什么地方添加它?或者这是在单独文件中创建的新集合?
  • 有没有办法在修改现有代码的同时做到这一点?
  • 哦,是否可以从文件系统文件夹而不是服务器根目录执行此操作?即遍历fileSystem文件夹的子目录和目录,而不是['DOCUMENT_ROOT']我该怎么做?
  • 是的,这将适用于文件系统,而不仅仅是在文档根目录中
猜你喜欢
  • 2012-11-14
  • 2012-09-02
  • 2014-10-16
  • 1970-01-01
  • 2011-03-01
  • 2014-09-12
  • 2014-03-08
相关资源
最近更新 更多