1

パスから、すべてのサブ (およびサブサブ) ディレクトリとすべてのファイル (最後のサブ) 内のすべてのファイル (.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-folder1たとえばsub-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);
4

1 に答える 1

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にあります。

それが助けになることを願っています。

于 2015-08-11T10:54:56.390 に答える