因爲我真的不能確定你的代碼的最終結果很難有效地回答,但解決您不妨考慮一個recursiveIterator
類型的方法嵌套的文件夾的問題。下面的代碼應該給你一個很好的起點 - 它需要一個目錄$dir
,並且將遍歷它並且它是子節點。
/* Start directory */
$dir='c:/temp2';
/* Files & Folders to exclude */
$exclusions=array(
'oem_no_drivermax.inf',
'smwdm.sys',
'file_x',
'folder_x'
);
$dirItr = new RecursiveDirectoryIterator($dir);
$filterItr = new DirFileFilter($dirItr, $exclusions, $dir, 'all');
$recItr = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST);
foreach($recItr as $filepath => $info){
$key = realpath($info->getPathName());
$filename = $info->getFileName();
echo 'Key = '.$key . ' ~ Filename = '.$filename.'<br />';
}
$dirItr = $filterItr = $recItr = null;
支持類
class DirFileFilter extends RecursiveFilterIterator{
protected $exclude;
protected $root;
protected $mode;
public function __construct($iterator, $exclude=array(), $root, $mode='all'){
parent::__construct($iterator);
$this->exclude = $exclude;
$this->root = $root;
$this->mode = $mode;
}
public function accept(){
$folpath=rtrim(str_replace($this->root, '', $this->getPathname()), '\\');
$ext=strtolower(pathinfo($this->getFilename(), PATHINFO_EXTENSION));
switch($this->mode){
case 'all':
return !(in_array($this->getFilename(), $this->exclude) or in_array($folpath, $this->exclude) or in_array($ext, $this->exclude));
case 'files':
return ($this->isFile() && (!in_array($this->getFilename(), $this->exclude) or !in_array($ext, $this->exclude)));
break;
case 'dirs':
case 'folders':
return ($this->isDir() && !(in_array($this->getFilename(), $this->exclude)) && !in_array($folpath, $this->exclude));
break;
default:
echo 'config error: ' . $this->mode .' is not recognised';
break;
}
return false;
}
public function getChildren(){
return new self($this->getInnerIterator()->getChildren(), $this->exclude, $this->root, $this->mode);
}
}