PHP scandir递归 [英] PHP scandir recursively

查看:71
本文介绍了PHP scandir递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的脚本递归扫描目录,

I'd like my script to scandir recursively,

$files = scandir('/dir');
foreach($files as $file){
if(is_dir($file)){
    echo '<li><label class="tree-toggler nav-header"><i class="fa fa-folder-o"></i>'.$file.'</label>';
    $subfiles = scandir($rooturl.'/'.$file);
        foreach($subfiles as $subfile){
            // and so on and on and on
        }
        echo '<li>';
    } else {
        echo $file.'<br />';
    }
}

我想以某种方式循环此操作:对于scandir找到的每个目录,它将在该目录中找到的文件夹上运行另一个scandir,

I'd like to loop this in a way that for each dir found by scandir, it runs another scandir on the folders that have been found within that dir,

因此目录'A'包含目录1/2/3,因此它现在应该为scandir(1),scandir(2),scandir(3) 等等,找到的每个目录.

So dir 'A' contains dir 1/2/3, it should now scandir(1), scandir(2), scandir(3) and so on for each dir found.

如何在不将代码重复粘贴到每个foreach中的情况下轻松实现这一目标?

How can I manage to implement this easily without copy pasting the code over and over in every foreach?

由于答案与我已经尝试过的几乎相同,因此我将对问题进行一些更新.

Since the answers are nearly the very same as I already tried, I'll update the question a bit.

使用此脚本,我需要创建一个树视图列表.在当前发布的脚本中,发生以下回显:

With this script I need to create a treeview list. With the current posted scripts the following get's echo'd happens:

/images/dir1/file1.png
/images/dir1/file2.png
/images/dir1/file3.png
/images/anotherfile.php
/data/uploads/avatar.jpg
/data/config.php
index.php

我真正需要的是:

<li><label>images</label>
    <ul>
        <li><label>dir1</label>
            <ul>
                <li>file1.png</li>
                <li>file2.png</li>
                <li>file3.png</li>
            </ul>
        </li>
        <li>anotherfile.php</li>
    </ul>
</li>
<li><label>data</label>
    <ul>
        <li><label>uploads</label>
            <ul>
                <li>avatar.jpg</li>
            </ul>
        </li>
        <li>config.php</li>
    </ul>
</li>
<li>index.php</li>

以此类推,谢谢您已经发布的答案!

And so on, Thank you for the already posted answers!

推荐答案

您可以通过这种方式递归扫描目录,目标是您的最顶层目录:

You can scan directory recursively in this way the target is your top most directory:

function scanDir($target) {

        if(is_dir($target)){

            $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned

            foreach( $files as $file )
            {
                scanDir( $file );
            }


        } 
    }

您可以轻松地根据需要调整此功能. 例如,如果要使用它删除目录及其内容,则可以执行以下操作:

You can adapt this function for your need easily. For example if would use this to delete the directory and its content you could do:

function delete_files($target) {

        if(is_dir($target)){

            $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned

            foreach( $files as $file )
            {
                delete_files( $file );
            }

            rmdir( $target );

        } elseif(is_file($target)) {

            unlink( $target );
    }

您无法按照自己的方式进行操作. 以下函数递归地获取所有目录,子目录以及所需的内容:

You can not do this in the way you are doing. The following function gets recursively all the directories, sub directories so deep as you want and the content of them:

function assetsMap($source_dir, $directory_depth = 0, $hidden = FALSE)
    {
        if ($fp = @opendir($source_dir))
        {
            $filedata   = array();
            $new_depth  = $directory_depth - 1;
            $source_dir = rtrim($source_dir, '/').'/';

            while (FALSE !== ($file = readdir($fp)))
            {
                // Remove '.', '..', and hidden files [optional]
                if ( ! trim($file, '.') OR ($hidden == FALSE && $file[0] == '.'))
                {
                    continue;
                }

                if (($directory_depth < 1 OR $new_depth > 0) && @is_dir($source_dir.$file))
                {
                    $filedata[$file] = assetsMap($source_dir.$file.'/', $new_depth, $hidden);
                }
                else
                {
                    $filedata[] = $file;
                }
            }

            closedir($fp);
            return $filedata;
        }
        echo 'can not open dir';
        return FALSE;
    }

将您的路径传递给函数:

Pass your path to the function:

$path = 'elements/images/';
$filedata = assetsMap($path, $directory_depth = 5, $hidden = FALSE);

$ filedata是一个包含所有已建立目录和子目录及其内容的数组.此功能使您可以根据需要深入扫描目录结构($ directory_depth),并清除所有无聊的隐藏文件(例如'.','..')

$filedata is than an array with all founded directories and sub directories with their content. This function lets you scan the directories structure ($directory_depth) so deep as you want as well get rid of all the boring hidden files (e.g. '.','..')

您现在要做的就是使用返回的数组(即完整的树结构),根据需要在视图中排列数据.

All you have to do now is to use the returned array, which is the complete tree structure, to arrange the data in your view as you like.

实际上,您正在尝试做的是一种文件管理器,并且您知道,其中有很多文件是在野外,开源和免费的.

What you are trying to do is in fact a kind of file manager and as you know there are a lot of those in the wild, open source and free.

希望这对您有所帮助,并祝您圣诞节快乐.

I hope this will help you and I wish you a merry Christmas.

这篇关于PHP scandir递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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