how do I exclude non-folders files from this readdir function?

徘徊边缘 提交于 2019-12-25 04:56:34

问题


The following lists the folders, the index.php and the favicon.ico in the directory. I want to see only folders.

Any ideas?

Thanks.

   <?php
     // opens this directory
     $myDirectory = opendir(".");

     // gets each entry
     while($entryName = readdir($myDirectory)) {
       $dirArray[] = $entryName;
     }

     // closes directory
     closedir($myDirectory);

     //  counts elements in array
     $indexCount   = count($dirArray);

     // sorts files
     sort($dirArray);

     // print 'em
     print("<table width='100%' cellspacing='10'>
             <tr>
               </tr>\n");

     // loops through the array of files and print them all
     for($index=0; $index < $indexCount; $index++) {
           if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
           print("<tr><td><a href='$dirArray[$index]'>$dirArray[$index]</a></td>");
           print("</tr>\n");
       }
     }
     print("</table>\n");
   ?>

回答1:


Use the following:

 // gets each entry
 while($entryName = readdir($myDirectory)) {
   if(is_dir($entryName)) {
     $dirArray[] = $entryName;
   }
 }

I however suggest to use glob() for this kind of operation. e.g.:

glob($dir . '/*', GLOB_ONLYDIR)


来源:https://stackoverflow.com/questions/3713588/how-do-i-exclude-non-folders-files-from-this-readdir-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!