2010-06-30 9 views
2

저는 파일 관리자 프로젝트를 진행하고 있으며 디렉토리 만 가져 오는 재귀 적 디렉토리 목록을 작성하는 것에 대해 머리를 맞지 않는 것처럼 보입니다. 내가 시도한 모든 것은 빈 서브 디렉토리를 벗어나거나 jQuery 트리의 리턴 구조로 파싱하려고 할 때 작동하지 않을 것이다.PHP 재귀 적 디렉토리 목록 - 디렉토리에만 해당

배열을 사용해 보았지만 재귀 적 반복기가 있지만 지금까지는 아무 것도 작동하지 않습니다.

** 업데이트 **

나는 아래로 내 코드를 깍았다 한 모든 사람의 입력에

감사 :

class image_management { 

private $_user_id; 
private $_user_name; 
private $_user_path;  

/** 
* Constructor 
*/ 
function __construct($user_id, $user_name) { 

    $this->_user_id = $user_id; 
    $this->_user_name = $user_name; 
    $this->_user_path = ORDER_ROOT.$this->_user_id; 
}  

/** 
* Cleanup the class and close connections 
* 
*/ 
function __destruct() { 
} 

/** 
* Get Image Folders 
* Returns an HTML list of the folders under a users 
* directory. 
* 
* @param hash $user_id -user id hash 
* @param string $user_name - users email address 
* @return string - html list 
*/ 
public function getHTMLTree() { 

    $html = "<li><a href='#'>"; 
    $html .= $this->_user_name . "</a><ul>"; 

    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->_user_path), RecursiveIteratorIterator::SELF_FIRST); 
    foreach($objects as $file) { 
    if($file->isDir()) { 
     $item = substr($file->getPathname(),strlen($this->_user_path)); 
     echo $item; 
    } 
    } 
    return $html; 
} 

}

이 나에게 출력 제공 :

/home/printshi/public_html/test/orders/88a0a01afd3728af8f6710ed874267f3 
/Aggie_Stuff 
/Misc 
/Misc/Christmas 
/Misc/Christmas/santa 
/Misc/Christmas/frosty 
/Misc/test1 
/Geek_Stuff 

가 더 가까워지고있다. 내 문제는 지금 내가해야 할 일은 HTML에서 이러한 모든 요소를 ​​감싸는 것이다.

<ul> 
<li>Aggie_Stuff</li> 
<li>Misc<ul> 
    <li>Christmas<ul> 
     <li>santa</li> 
     <li>frosty</li> 
     </ul> 
    </li> 
    <li>test1</li> 
    </ul> 
<li>Geek_Stuff</li> 

이해 재귀 부분은 그냥 내가하려고 아무리 열심히 주위에 내 머리를 얻이 수없는 것 HTML을 할 점점 : 모자 나 출력 같이 얻을.

+0

[가시성을 키워드] (http://www.php.net/manual/en/language.oop5.visibility.php)를 메소드 앞에두고 속성에 대한 'var' 키워드를 사용하십시오. – Charles

+0

누락 된 링크에 대한 내 대답을 참조하십시오 : http://stackoverflow.com/questions/2207599/multidimensional-array-iteration/2207739#2207739 - ''을 하드 코딩하는 대신'$ this-> key()'를 사용합니다 – Gordon

답변

6

DirectoryIterator 클래스, 특히 isDir 기능을 살펴보십시오.

$iterator = new DirectoryIterator(dirname(__FILE__)); 
foreach ($iterator as $fileinfo) { 
    if ($fileinfo->isDir() && !$fileinfo->isDot()) { 
     echo $fileinfo->getFilename() . "\n"; 
     // recursion goes here. 
    } 
} 
2

RecursiveIteratorIteratorRecursiveDirectoryIterator 당신을 위해이 작업을 수행 할 수 있습니다

당신이, 당신이 사용을 고려하시기 바랍니다 PHP 4에서 매우 명확하게 객체 스타일을 사용하고이 PHP 5를 태그 한 동안
$iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); 
foreach($iter as $file) { 
    if ($file->getFilename() == '.') { 
     echo $file->getPath() . PHP_EOL; 
    } 
} 
+0

http://stackoverflow.com/questions/24121723/multidimensional-directory-list-with-recursive-iterator에 대한 도움을 드릴 수 있습니까? – YahyaE