什么是获取所有子目录的列表以及php中给定目录下所有文件的另一个列表的最佳方法。 我可以使用非纯PHP代码,只要我可以从php使用它(例如ac / java / python / ...程序)。 比纯recursion快的东西,某种语言内置的东西(因为这些东西往往很快。)

===============>>#1 票数:4

看看RecursiveDirectoryIterator

foreach (new RecursiveDirectoryIterator('yourDir') as $file) {
    // you don't want the . and .. dirs, do you?
    if ($file->isDot()) {
        continue;
    }

    if ($file->isDir()) {
        // dir
    } else {
        // file
    }
}

===============>>#2 票数:2 已采纳

而且,如果您不喜欢OOing事情,则可以通过查找结果运行opendir()循环。

if (exec('find /startdir -type d -print', $outputarray)) {
  foreach ($outputarray as $onepath) {
    // do stuff in $onepath
  }
}

您确实指定了“非纯PHP”作为选项,对吗? :-)

===============>>#3 票数:1

取自php.netglob()文档:

$path[] = 'starting_place/*';

while(count($path) != 0) {
  $v = array_shift($path);

  foreach(glob($v) as $item) {
    if(is_dir($item))
      $path[] = $item . '/*';
    else if (is_file($item)) {
      //do something
    }
  }
}

===============>>#4 票数:1

class Dir_helper{
    public function __construct(){

    }
    public function getWebdirAsArray($rootPath){
        $l1 = scandir($rootPath);
        foreach ($this->getFileList($rootPath) as $r1){
        if ($r1['type'] == 'dir'){
            if (preg_match("/\./", $r1['name'])){
            $toplevel[] =  $r1['name'];
            } else {
            if (preg_match("/\d/",$r1['name'])){
                $seclevel[] = $this->getFileList($r1['name']);
            }
            }
        }
        }
        foreach ($seclevel as $sl){
        foreach ($sl as $cur){
            $sub[] = $cur['name'];
        }
        }
        return $result = array_merge((array)$toplevel, (array)$sub);
    }

    public function getFileList($dir){
        $retval = array();
        if(substr($dir, -1) != "/") $dir .= "/";
        $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
        while(false !== ($entry = $d->read())) {
            if($entry[0] == ".") continue;
            if(is_dir("$dir$entry")) {
                $retval[] = array(
                "name" => "$dir$entry/",
                "type" => filetype("$dir$entry"),
                "size" => 0,
                "lastmod" => filemtime("$dir$entry")
                );
            } elseif(is_readable("$dir$entry")) {
                $retval[] = array(
                "name" => "$dir$entry",
                "type" => mime_content_type("$dir$entry"),
                "size" => filesize("$dir$entry"),
                "lastmod" => filemtime("$dir$entry")
                );
            }
        }
        $d->close();
        return $retval;
    }

}

===============>>#5 票数:0

由于您不希望递归,因此我只写了一些额外的内容

// $dirs = [];
// Get All Files & Folders in $dir
$files = glob("$dir/*");
for ($i=0; $i < count($files); $i++) { 
    if (is_dir($files[$i])) {
        $files = array_merge($files, glob("$files[$i]/*"));
    //  $dirs[] = $files[$i]; // This can add the folder to a dir array
    }
}
// Remove folders from the list if you like
foreach ($files as $key => $file) {
    if (is_dir($file)) {
        unset($files[$key]);
    }
}
// Clean up the key numbers if you removed files or folders
$files = array_values($files);

===============>>#6 票数:0

使用php的内置RecursiveDirectoryIterator

编辑

就像是:

$dirs  = array();
$files = array();

$dir = __DIR__ . '/foo';

$iterator = new RecursiveDirectoryIterator(new DirectoryIterator($dir));

foreach ($iterator as $dirElement) {
    if ($dirElement->isDir()) {
        $dirs[] $dirElement->getPathname();
    }
    if ($dirElement->isFile()) {
        $files[] = $dirElement->getPathname();
    }
}

  ask by prongs translate from so

未解决问题?本站智能推荐:

关注微信公众号