freaky
freaky

Reputation: 1010

php - get sub-sub-folder and specific files name and path

I would to retrieve from a path all sub (and sub-sub) directories and all files (only with .php and .css extensions) in (last sub) it.

The structre is the following:

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

I know the my_pathdirectory name. About sub-folder, I only want to retrieve sub-folder which have the name (sub-folder1, sub-folder2 for example). And I would like to retrieve all sub-sub-folders and inside them and get the name and path of the .css and .php files inside it. I'm not sure if I'm clear with my explanations.

My aim is to store all this information in an array like this:

$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',
   ),
   ...
);

I have started with this code but I have some difficulties to make it works (and it's not finished), I don't use this kind of functionnality a lot...

$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);

Upvotes: 1

Views: 1099

Answers (1)

Debflav
Debflav

Reputation: 1151

I like using the RecursiveDirectoryIterator class. I think it's more readable. This sample allow you to read all subfolders of a given source.

$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();
}

You can find the whole list of available functions at Directory Iterator.

Hope it's help.

Upvotes: 1

Related Questions