简体   繁体   English

PHP函数读取目录并将文件名和子目录名称收集到数组中?

[英]PHP function to read directory & collect filenames &subdirectory names into an array?

I have been trying to find the correct PHP function to do the following: 我一直在寻找正确的PHP函数来执行以下操作:

PHP function that reads a directory and collects all the filenames (and subdirectory names) into an array. PHP函数读取目录并将所有文件名(和子目录名称)收集到一个数组中。

I have been searching for a while, not to sure on what exactly is the correct answer. 我已经搜索了一段时间,不确定确切的答案是什么。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks. 谢谢。

See http://www.php.net/manual/en/function.opendir.php - to open directory 参见http://www.php.net/manual/en/function.opendir.php-打开目录

Links to read directory. 链接到读取目录。

Would imagine there is an example 可以想象有一个例子

see this question 看到这个问题

<?php 
function dirToArray($dir) { 

   $result = array(); 

   $cdir = scandir($dir); 
   foreach ($cdir as $key => $value) 
   { 
      if (!in_array($value,array(".",".."))) 
      { 
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
         { 
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
         } 
         else 
         { 
            $result[] = $value; 
         } 
      } 
   } 

   return $result; 
} 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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