0
function file_list($path){
    $final_result=array();
    if ($handle = opendir($path)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if (is_dir($path."/".$file)) {
                    //echo $path."/".$file."\n";//directory
                    return file_list($path."/".$file); 
                } else {
                    //echo $path."/".$file."\n"; //all file 
                    if(stripos($path."/".$file,"playlist.data"))
                    {
                        //echo $path."/".$file."\n"; //file contains "list.data"
                        $content = file_get_contents($path."/".$file);
                        preg_match_all("/([0-9]*).txt,0/",$content, $matches);
                        $result=array_unique($matches[1]);
                        $final_result=array_merge($final_result,$result);
                        $final_result=array_unique($final_result);
                        sort($final_result);
                        return($final_result);
                    }
                }
            } 
        } 
    } 
} 
print_r(file_list($argv[1]));

次のようなlist.dataファイル:

1.txt
3.txt

次のような別のlist.dataファイル:

2.txt
4.txt

したがって、結果は次のような配列になります。

Array(
    [0]=>1
    [1]=>2
    [2]=>3
    [3]=>4
)

ファイルシステムの指定されたディレクトリを再帰的に検索することを目指していましたが、「list.data」というファイル名を見つけたとき、このファイルを読み取り、正規表現を使用してフィルタリングし、$result. 複数の「list.data」があるため、すべて$resultをにマージしたかったの$final_resultです。しかし、このスクリプトは何も出力しません。関数に何か問題があると誰か教えてくださいfile_list

この php スクリプトを CLI モードで次のように実行しています: php.exe script.php "d:/test/listdir"

4

2 に答える 2

4

This is a paraphrased pseudo-code version showing the recursive merging logic:

function file_list($path) {
    $result = array();

    foreach ($path as $file) {
        if (is_dir($file)) {
            $result = array_merge($result, file_list($file));
        } else {
            $result[] = ...
        }
    }

    return $result;
}

The function always returns an array, even if it's empty. Going through a directory, you add your results into the $result array. If you encounter a subdirectory, you let file_list go through it, merging the results into your current $result array (because file_list returns an array of results). Finally, after all entries in the directory have been handled, you return the list of results to the caller (which may be file_list itself).

If you feel like experimenting, look into the RecursiveDirectoryIterator, which would allow you to flatten the logic in your function, making it non-recursive.

于 2011-07-22T06:03:28.013 に答える
0

やろうとしていることを実行する最善の方法: PHP に再帰を任せます。

$iter = new RecursiveIteratorIterator( 
           new RecursiveDirectoryIterator( 
                $path, FilesystemIterator::CURRENT_AS_FILEINFO ) );

$out = array();
foreach( $iter as $file )
{
    if( $file->getBasename() == 'list.data' )
    {
        $listdata = file_get_contents( $file->getPathname() );
        // do something with list.data
        $out = array_merge( $listDataOutput, $out );
    }
}
于 2011-07-22T06:13:39.717 に答える