简体   繁体   中英

how get all folder subfolder only without filename using php

i want to get all list folder and subfolder only without file name and with filename example output:

folder name:
source/1
source/1/2
source/1/2/3

filename
0.jpg
1.jpg
3.jpg

this my code, this code work fine , but this code show filename to..

$rootpath = 'source';
$fileinfos = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootpath)
);
foreach($fileinfos as $pathname => $fileinfo) {
    if (!$fileinfo->isFile()) continue;

echo($pathname).'<br>';


}

edit: how to using regex or preg-match foldername/subfolder/filename.jpg

how to get only foldername/subfolder/ and only filename.jpg

$filepath = 'foldername/subfolder/filename.jpg';

$folder= (preg_match('~[^A-Za-z0-9_\./\]~', $filepath));
echo 'folder:' .'<br>' .$folder;

$filename =(preg_match('....', $filepath));

echo 'fileneme' .'<br>' .$filename;

output

folder:
foldername/subfolder/

filename:
filename.jpg

thank

The snippet below may help. If you decide to get only FILES ; just pass the String 'file' as the 3rd Argument to the Function scanDirRecursive . By default, the function returns a listing of all DIRECTORIES and SUB-DIRECTORIES in the given directory: $directory (1st Argument). However; you may still get both the DIRECTORIES & the FILES in one go.
To do that; just pass the string: both as the 3rd Argument....

Hope this helps little.
CHEERS & GOOD-LUCK!!!

    <?php

        $rootPath       = 'source';
        $regex          = null;

        /**
         * @param string $directory    => DIRECTORY TO SCAN
         * @param string $regex        => REGULAR EXPRESSION TO BE USED IN MATCHING FILE-NAMES
         * @param string $get          => WHAT DO YOU WANT TO GET? 'dir'= DIRECTORIES, 'file'= FILES, 'both'=BOTH FILES+DIRECTORIES
         * @param bool   $useFullPath  => DO YOU WISH TO RETURN THE FULL PATH TO THE FOLDER OR JUST ITS NAME?
         * @param array  $dirs         => LEAVE AS IS: USED DURING RECURSIVE TRIPS
         * @return array
         */
        function scanDirRecursive($directory, $regex=null, $get="dir", $useFullPath=false,  &$dirs=[], &$files=[]) {
            $iterator               = new DirectoryIterator ($directory);
            foreach($iterator as $info) {
                $fileDirName        = $info->getFilename();

                if ($info->isFile () && !preg_match("#^\..*?#", $fileDirName)) {
                    if($get == 'file' || $get == 'both'){
                        if($regex) {
                            if(preg_match($regex, $fileDirName)) {
                                if ($useFullPath) {
                                    $files[] = $directory . DIRECTORY_SEPARATOR . $fileDirName;
                                }
                                else {
                                    $files[] = $fileDirName;
                                }
                            }
                        }else{
                            if($useFullPath){
                                $files[]   = $directory . DIRECTORY_SEPARATOR . $fileDirName;
                            }else{
                                $files[]   = $fileDirName;
                            }
                        }
                    }
                }else if ($info->isDir()  && !$info->isDot()) {
                    $fullPathName   = $directory . DIRECTORY_SEPARATOR . $fileDirName;
                    if($get == 'dir' || $get == 'both') {
                        $dirs[]     = ($useFullPath) ? $fullPathName : $fileDirName;
                    }
                    scanDirRecursive($fullPathName, $regex, $get, $useFullPath, $dirs, $files);
                }
            }

            if($get == 'dir') {
                return $dirs;
            }else if($get == 'file'){
                return $files;
            }
            return ['dirs' => $dirs, 'files' => $files];
        }

        // GET ONLY DIRECTORIES IN THE 'source' FOLDER + SUB-FOLDERS
        $directories    = scanDirRecursive($rootPath, $regex, 'dir', false);

        // GET ONLY FILES IN THE 'source' FOLDER + SUB-FOLDERS
        $files          = scanDirRecursive($rootPath, $regex, 'file', false);

        // GET BOTH FILES & DIRECTORIES IN THE 'source' FOLDER + SUB-FOLDERS
        $both           = scanDirRecursive($rootPath, $regex, 'both', false);

        var_dump($directories);
        var_dump($files);
        var_dump($both);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM