簡體   English   中英

遞歸獲取目錄中的所有文件,並按擴展名獲取子目錄

[英]Recursivly get all files in a directory, and sub directories by extension

我看着RecursiveDirectoryIteratorglob

,然后根據擴展名(例如)“。less返回文件列表(以數組形式)。哦,查找所有子代,孫代等等,不包括...直到找到所有匹配的文件。

但是我不確定創建一個遞歸函數的最佳方法,這種遞歸函數不會超出大孩子。

我有一個爛攤子,它已經工作了兩年-但現在我需要重構和更改它:

public function get_directory_of_files($path, $filename, $extension) {
        if (!is_dir($path)) {
            throw new AisisCore_FileHandling_FileException("Could not find said path: " . $path);
        }

        if (file_exists($filename)) {
            $handler = opendir($path);
            while ($file = readdir($handler)) {
                if ($file != "." && $file != "..") {
                    $this->package_files [] = $file;
                    $count = count($this->package_files);
                    for ($i = 0; $i < $count; $i++) {
                        if (substr(strrchr($this->package_files [$i], '.'), 1) == $extension) {
                            if ($this->package_files [$i] == $filename) {
                                $this->files_got_back = $this->package_files [$i];
                            }
                        }
                    }
                }
            }
        }

        return $this->_files_got_back;
    }

這需要傳遞一個文件名,這實際上不是我要做的任何事情。 因此,我該如何重寫此函數以執行上面的“ 偽代碼

看一下這段代碼:

<?php

class ex{

    private function get_files_array($path,$ext, &$results){ //&to ensure it's a reference... but in php obj are passed by ref.

        if (!is_dir($path)) {
            //throw new AisisCore_FileHandling_FileException("Could not find said path: " . $path);
        }

        if ($dir = opendir($path)) {
            $extLength = strlen($ext);

            while (false !== ($file = readdir($dir))) {
                if ($file != '.' && $file != '..'){
                    if (is_file($path.'/'.$file) && substr($file,-$extLength) == $ext){
                        $results[] = $path . '/' . $file; //it's a file and the correct extension
                    }
                    elseif (is_dir($path . '/'. $file)){ 
                        $this->get_files_array($path.'/'.$file, $ext, $results); //it's a dir
                    }               
                }
            }

        }else{
            //unable to open dir
        }
    }

    public function get_files_deep($path,$ext){
        $results = array();
        $this->get_files_array($path,$ext,$results);
        return $results;
    }

}

    $ex = new ex();

    var_dump($ex->get_files_deep('_some_path','.less'));

?>

它將檢索路徑及其子目錄中具有匹配擴展名的所有文件。

希望這就是您所需要的。

此函數遞歸查找具有匹配的結束字符串的文件

function getDirectoryContents($directory, $extension)
{
    $extension = strtolower($extension);
    $files = array();
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

    while($it->valid()) 
    {
        if (!$it->isDot() && endsWith(strtolower($it->key()), $extension))
        {
            array_push($files, $it->key());
        }
        $it->next();
    }
    return $files;
}
function endsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}

像這樣使用print_r(getDirectoryContents('folder /','.php'));

它將擴展名轉換為小寫以與

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM