想要glob api的正则表达式来查找文件

My directory structure is like D:/dir1/dir2/project_dir/dir3/dir4/dir5/cache/all_files

So i want to get all the files under cache folder. So i wrote

glob("project_dir/*/*/*/cache/*");

But cache folder is also there in dir3 or dir4 like

D:/dir1/dir2/project_dir/dir3/dir4/dir5/dir6/dir7/cache/all_files

or

D:/dir1/dir2/project_dir/dir3/cache/all_files

So can anyone give me the regex to get the files from 'cache' folder, like

glob("project_dir/*/cache/*");

Btw, this is not working because it is searching for immediate directory after project_dir.

Thanks in advance.

You can use glob('*') but the only perk for using this is it ignores all hidden files. If you want all the files that includes hidden files in a folder then use:

$files = glob('project_dir/cache/{,.}*', GLOB_BRACE);

Remember this will return all the files for "." and "" and even the directory entries . and .. Let me know in case of any issues.

Hope this helps.

Hope this helps!

Supports PHP 5 and 7 only,check your version before using this code

for more info refer : http://php.net/manual/en/class.directoryiterator.php

    $searchCacheFolderUnder = 'D:\xampp\htdocs\mygit\\'; 
    $pathOfAllCacheFolders = array();
    $dir = new DirectoryIterator(realpath($searchCacheFolderUnder));
    foreach ($dir as $fileInfo) {
        if($fileInfo->isDir()) {
            // optimize strpos with inbuild directory iterator methods
            if( strpos($fileInfo->getPathname(), 'cache') !== false ){
                $pathOfAllCacheFolders[] = $fileInfo->getPathname();
            }

        }
    }

    // contains all cache folders path
    echo "<pre>";
    print_r($pathOfAllCacheFolders);
    echo "</pre>";

    // loop on all cache folders
    foreach($pathOfAllCacheFolders as $cachePath)
    $dirCache = new DirectoryIterator(realpath($cachePath));
    foreach ($dirCache as $fileInfo) {
        //$fileInfo object will have everything you need
        echo "<pre>";
        print_r($fileInfo);
        echo "</pre>";
    }

You have to modify this code a bit to meet your requirement. Refer Directory Iterator for more available options.