如何从此readdir函数中排除非文件夹文件? [英] how do I exclude non-folders files from this readdir function?

查看:184
本文介绍了如何从此readdir函数中排除非文件夹文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下列出了目录中的文件夹,index.php和favicon.ico.我只想查看文件夹.

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

有什么想法吗?

谢谢.

   <?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");
   ?>

推荐答案

使用以下内容:

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

但是我建议对这种操作使用glob().例如:

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

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

这篇关于如何从此readdir函数中排除非文件夹文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆