5

我现在有点迷失在这里。我的目标是递归扫描每个子文件夹中包含子文件夹和图像的文件夹,将其放入多维数组中,然后能够解析每个子文件夹及其包含的图像。

我有以下起始代码,它基本上是扫描每个包含文件的子文件夹,现在只是丢失了将它放入一个多数组中。

$dir = 'data/uploads/farbmuster';
$results = array();

if(is_dir($dir)) {
    $iterator = new RecursiveDirectoryIterator($dir);

    foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
        if($file->isFile()) {
            $thispath = str_replace('\\','/',$file->getPath());
            $thisfile = utf8_encode($file->getFilename());

            $results[] = 'path: ' . $thispath. ',  filename: ' . $thisfile;
        }
    }
}

有人可以帮我弄这个吗?

提前致谢!

4

3 回答 3

10

你可以试试

$dir = 'test/';
$results = array();
if (is_dir($dir)) {
    $iterator = new RecursiveDirectoryIterator($dir);
    foreach ( new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file ) {
        if ($file->isFile()) {
            $thispath = str_replace('\\', '/', $file);
            $thisfile = utf8_encode($file->getFilename());
            $results = array_merge_recursive($results, pathToArray($thispath));
        }
    }
}
echo "<pre>";
print_r($results);

输出

Array
(
    [test] => Array
        (
            [css] => Array
                (
                    [0] => a.css
                    [1] => b.css
                    [2] => c.css
                    [3] => css.php
                    [4] => css.run.php
                )

            [CSV] => Array
                (
                    [0] => abc.csv
                )

            [image] => Array
                (
                    [0] => a.jpg
                    [1] => ab.jpg
                    [2] => a_rgb_0.jpg
                    [3] => a_rgb_1.jpg
                    [4] => a_rgb_2.jpg
                    [5] => f.jpg
                )

            [img] => Array
                (
                    [users] => Array
                        (
                            [0] => a.jpg
                            [1] => a_rgb_0.jpg
                        )

                )

        )

使用的功能

function pathToArray($path , $separator = '/') {
    if (($pos = strpos($path, $separator)) === false) {
        return array($path);
    }
    return array(substr($path, 0, $pos) => pathToArray(substr($path, $pos + 1)));
}
于 2012-10-19T14:02:27.743 回答
2

RecursiveDirectoryIterator以递归方式扫描到平面结构中。要创建深层结构,您需要使用DirectoryIterator的递归函数 (调用自身)。如果你当前的文件是Dir () 和!isDot () 通过以新目录作为参数再次调用该函数来深入了解它。并将新数组附加到您当前的集合中。

如果你不能处理这个叫喊,我会在这里转储一些代码。必须记录一下(现在有忍者评论),所以......用懒惰的方式试试我的运气,并附上说明。

代码

/**
 * List files and folders inside a directory into a deep array.
 *
 * @param string $Path
 * @return array/null
 */
function EnumFiles($Path){
    // Validate argument
    if(!is_string($Path) or !strlen($Path = trim($Path))){
        trigger_error('$Path must be a non-empty trimmed string.', E_USER_WARNING);
        return null;
    }
    // If we get a file as argument, resolve its folder
    if(!is_dir($Path) and is_file($Path)){
        $Path = dirname($Path);
    }
    // Validate folder-ness
    if(!is_dir($Path) or !($Path = realpath($Path))){
        trigger_error('$Path must be an existing directory.', E_USER_WARNING);
        return null;
    }
    // Store initial Path for relative Paths (second argument is reserved)
    $RootPath = (func_num_args() > 1) ? func_get_arg(1) : $Path;
    $RootPathLen = strlen($RootPath);
    // Prepare the array of files
    $Files = array();
    $Iterator = new DirectoryIterator($Path);
    foreach($Iterator as /** @var \SplFileInfo */ $File){
        if($File->isDot()) continue; // Skip . and ..
        if($File->isLink() or (!$File->isDir() and !$File->isFile())) continue; // Skip links & other stuff
        $FilePath = $File->getPathname();
        $RelativePath = str_replace('\\', '/', substr($FilePath, $RootPathLen));
        $Files[$RelativePath] = $FilePath; // Files are string
        if(!$File->isDir()) continue;
        // Calls itself recursively [regardless of name :)]
        $SubFiles = call_user_func(__FUNCTION__, $FilePath, $RootPath);
        $Files[$RelativePath] = $SubFiles; // Folders are arrays
    }
    return $Files; // Return the tree
}

测试它的输出并弄清楚:)你可以做到!

于 2012-10-19T12:55:25.657 回答
0

如果要获取带有子目录的文件列表,请使用(但更改文件夹名称)

<?php
$path = realpath('yourfold/samplefolder');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
        echo "$filename\n";
}
?>
于 2013-03-28T11:59:50.693 回答