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 に答える