简体   繁体   中英

Echo all the folder names in a folder

I want to print all the folder names inside a parent folder. The current issue I am facing is, though I have 400+ folders in a folder only 257 are getting printed. Again, this is not at all issue related with permissions.

Please find my code below:

    $newdir = "content/";
     $dircnt = 0;
// Open a known directory, and proceed to read its contents
if (is_dir($newdir)) {
    if ($dh = opendir($newdir)) {
       while (($file = readdir($dh)) !== false) {
           $dircnt++;
           if(filetype($newdir. $file) == 'dir') {
            echo "filename: $file : filetype: " . filetype($newdir. $file) . "dircnt:"        .$dircnt. "<br>";
          }
       }
       closedir($dh);
      }
   }
}

You can use glob() function - returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.

       $filesDirectories = glob($newdir.'*', GLOB_BRACE);
        foreach($filesDirectories as $key=>$file) {
             echo "$file size " . filesize($file) . "\n";               
       }

I would use glob:

$newdir = "content/";
$dirs = glob($newdir.'*',GLOB_ONLYDIR);
foreach($dirs as $index=>$dir){
    echo "filename ". $dir." filetype ".filetype($newdir.$dir)." dircnt:".($index+1)."<br/>";
}

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