dongpai6567 2011-11-15 16:06
浏览 43

PHP中的数组递归目录; 相对映射到绝对值

I'm pulling results from a recursive directory listing into an array, and was wondering if there is a better (read faster, briefer, etc.) way of doing it. Specifically, I'm creating an array of:

path_relative_to_somedir => absolute_path

Right now I've got:

$map = array();
$base_realpath = realpath('/path/to/dir');
$iterator = new \RecursiveDirectoryIterator($base_realpath);
foreach((new \RecursiveIteratorIterator($iterator)) as $node){
    $node_realpath = $node->getRealpath();
    $map[substr($node_realpath, strlen($base_realpath) + 1)] = $node_realpath;
}

This works fine (seemingly) but I'm worried about edge cases that, while I'm sure they will come out in testing, others may be able to point out. So:

  • Is there a better (read faster, more accurate, more reliable) way to get the node path relative to the $base_realpath?
  • Should I consider options such as glob(), or readdir() as faster alternatives? (this is a somewhat time sensitive operation, I saw a question pertaining to directory recursion in PHP with some benchmarks in an answer, will link when found)

--Question ends--


--Possibly unnecessary detail begins--

Purpose being; I'm creating a virtual working directory for an application, so that calls to a given file are mapped to the actual file. For example: (I'm elaborating in case someone has an overall better alternative approach based on what I'm actually doing)

Given dir1 is the "root" of the virtual working directory, and we wish to merge in dir2:

path/                       path/
|                           |
+-- to/                     +-- to/
    |                           |
    +-- dir1/                   +-- dir2/
        |                           |
        +-- script1.php             +-- script2.php
        |                           |
        +-- script2.php             +-- subdir/
        |                               | 
        +-- subdir/                     +-- script4.php
            |               
            +-- script3.php 

It would yield an array like so:

[script1.php] => path/to/dir1/script1.php
[script2.php] => path/to/dir2/script2.php
[subdir/script3.php] => path/to/dir1/subdir/script3.php
[subdir/script4.php] => path/to/dir2/subdir/script4.php

Notice that the merging replaces existing relative paths, and each element is mapped to it's actual path. I'm simply using array_replace() here, here's the method snippet:

public function mergeModule($name){
    $path = realpath($this->_application->getPath() . 'modules/' . $name);
    if(!is_dir($path) || !is_readable($path)){
        // @todo; throw exception
    }
    $map = array();
    try{
        $directory_iterator = new \RecursiveDirectoryIterator($path);
        foreach((new \RecursiveIteratorIterator($directory_iterator)) as $node){
            $node_realpath = $node->getRealpath();
            $map[substr($node_realpath, strlen($path) + 1)] = $node_realpath;
        }
    }catch(\Exception $exception){
        // @todo; handle
    }
    $this->_map = array_replace($this->_map, $map);
}
  • 写回答

1条回答 默认 最新

  • dqmq0654 2012-01-06 16:13
    关注

    If you are looking for a faster way to list directories recursively you can try with external programs instead. A lot faster :)

    exec('tree -if /var/www',$tree);
    foreach($tree as $key => $path) {
        if ( !$path ) // Ignore the summary ex "5 directories, 30 files"
           break;
    
        // Do something with the file/dir path
    }
    

    If you only need .php files you can use the find command.

    find . -name "*.php"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错