【问题标题】:php - get sub-sub-folder and specific files name and pathphp - 获取子子文件夹和特定文件名和路径
【发布时间】:2015-11-03 12:52:19
【问题描述】:

我会从路径中检索所有子(和子子)目录和所有文件(仅具有 .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);

【问题讨论】:

    标签: php file path glob dirname


    【解决方案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 找到可用功能的完整列表。

    希望对你有所帮助。

    【讨论】:

    • 感谢您的回答,但它不排除子文件夹,也不完全符合我的要求。 RecursiveDirectoryIterator 类是否更快?
    • 要知道文件夹名称,请使用$iterator->isDir() 的条件,然后使用$iterator->getFileName() 检查名称。我没有衡量它是否更快。我不这么认为,但它有助于保持 OOP 风格。
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2022-12-21
    • 1970-01-01
    相关资源
    最近更新 更多