php - 获取子子文件夹和特定文件的名称和路径

时间:2015-08-11 10:31:29

标签: php file path glob dirname

我想从一个路径中检索所有子(和子子)目录和所有文件(只有.php和.css扩展名)(最后一个子句)。

结构如下:

my_path/sub-folder1/            // I want to get the sub folder name (and check if allowed)
     sub-sub-folder1/file1.php  // I want to get the file name and path
     sub-sub-folder1/file1.css  // I want to get the file name and path
     sub-sub-folder2/file2.php  // ....
     sub-sub-folder2/file2.css
     sub-sub-folder3/file3.php
     sub-sub-folder3/file3.css
my_path/sub-folder2/
     sub-sub-folder1/file1.php
     sub-sub-folder1/file1.css
     sub-sub-folder2/file2.php
     sub-sub-folder2/file2.css
     sub-sub-folder3/file3.php
     sub-sub-folder3/file3.css

我知道my_path目录名称。关于子文件夹,我只想检索名称为sub-folder1sub-folder2的子文件夹。我想检索所有子子文件夹并在其中,并获取其中的.css和.php文件的名称和路径。我不确定我的解释是否清楚。

我的目标是将所有这些信息存储在这样的数组中:

$data['sub-folder-1']= array(
   'sub-sub-folder1' => array(
       'name' => 'file1',
       'php'  => 'file1.php',      
       'css'  => 'file1.css',
   ),
   'sub-sub-folder2' => array(
       'name' => 'file2',
       'php'  => 'file2.php',      
       'css' => 'file2.css',
   ),
   ...
);  
$data['sub-folder-2']= array(
   'sub-sub-folder1' => array(
       'name' => 'file1',
       'php'  => 'file1.php',      
       'css'  => 'file1.css',
   ),
   'sub-sub-folder2' => array(
       'name' => 'file2',
       'php'  => 'file2.php',      
       'css'  => 'file2.css',
   ),
   ...
);

我已经开始使用这段代码,但是我有一些困难使它工作(而且还没有完成),我不太多使用这种功能......

$dirname = 'my_path';
$findphp = '*.php';
$findcss = '*.css';
$types   = array('sub-folder1','sub-folder2');

$sub_dirs = glob($dirname.'/*', GLOB_ONLYDIR|GLOB_NOSORT);

if(count($sub_dirs)) {
    foreach($sub_dirs as $sub_dir) {
        $sub_dir_name = basename($sub_dir);
        if (in_array($sub_dir_name,$types)) {
            $sub_sub_dirs = glob($sub_dir.'/*', GLOB_ONLYDIR|GLOB_NOSORT);
            if(count($sub_sub_dirs)) {
                foreach($sub_sub_dirs as $sub_sub_dir) {
                    $php = glob($sub_sub_dir.'/'.$findphp);
                    $css = glob($sub_sub_dir.'/'.$findcss);
                    $sub_sub_dir_name =  basename($sub_sub_dir);
                    $data[$sub_dir_name][$sub_sub_dir_name]['type'] = $sub_dir_name;
                    $data[$sub_dir_name][$sub_sub_dir_name]['name'] = basename($php[0], '.php');
                    $data[$sub_dir_name][$sub_sub_dir_name]['html'] = $php[0];
                    $data[$sub_dir_name][$sub_sub_dir_name]['css']  = $css[0];
                }
            }
        }
    }
} else {
    echo "Nothing was found.";
}
print_r($data);

1 个答案:

答案 0 :(得分:1)

我喜欢使用RecursiveDirectoryIterator类。我认为它更具可读性。此示例允许您读取给定源的所有子文件夹。

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source)); // Top source directory
$iterator->setFlags(\FilesystemIterator::SKIP_DOTS); // Skip folders with dot

$allowedExtension = array('php', 'css'); // And other if needed
$skin = array();
while ($iterator->valid()) {

    $extension = $iterator->getExtension(); // >= PHP 5.3.6
    if ($iterator->isFile() && in_array($extension, $allowedExtension)) {
        // Complete this part or change it following your needs
        switch ($extension) {
            case 'php':
                // All your logic here
                $skin[$iterator->getPath()]['name'] = $iterator->getFileName(); //eg
                break;
            default:
                break;
        }
     }

     $iterator->next();
}

您可以在Directory Iterator找到可用功能的完整列表。

希望有所帮助。