2010-06-23 3 views
2

특정 디렉토리에 나열된 폴더의 선택 목록을 표시하기 위해 scandir을 사용하려고합니다 (잘 작동 함). 그러나 선택 목록에 하위 폴더 (있는 경우)도 추가해야합니다. 누구든지 나를 도울 수 있다면, 좋을 것입니다! php를 사용하여 하위 폴더를 포함하여 폴더 선택 목록을 만드시겠습니까?

내가 원하는 구조입니다 :

<option>folder 1</option> 
<option> --child 1</option> 
<option> folder 2</option> 
<option> folder 3</option> 
<option> --child 1</option> 
<option> --child 2</option> 
<option> --child 3</option> 

그리고 이것은 내가이 스레드에서 가지고 (단지 상위 폴더를 보여줍니다) 내가 가지고있는 코드 (Using scandir() to find folders in a directory (PHP))입니다 :

$dir = $_SERVER['DOCUMENT_ROOT']."\\folder\\"; 

$path = $dir; 
$results = scandir($path); 

$folders = array(); 
foreach ($results as $result) { 
    if ($result == '.' || $result == '..') continue; 
    if (is_dir($path . '/' . $result)) { 
     $folders[] = $result; 
    }; 
}; 

^^하지만 아이 디렉토리도 보여줄 필요가 있습니다 .. 누군가 도울 수 있다면, 좋을 것입니다! :)

편집 :

+0

그들을 재귀 적으로 통과합니다. – Andrey

답변

2
/* FUNCTION: showDir 
* DESCRIPTION: Creates a list options from all files, folders, and recursivly 
*  found files and subfolders. Echos all the options as they are retrieved 
* EXAMPLE: showDir(".") */ 
function showDir($dir , $subdir = 0) { 
    if (!is_dir($dir)) { return false; } 

    $scan = scandir($dir); 

    foreach($scan as $key => $val) { 
     if ($val[0] == ".") { continue; } 

     if (is_dir($dir . "/" . $val)) { 
      echo "<option>" . str_repeat("--", $subdir) . $val . "</option>\n"; 

      if ($val[0] !=".") { 
       showDir($dir . "/" . $val , $subdir + 1); 
      } 
     } 
    } 

    return true; 
} 
+0

고마워요,하지만 고마워요 파일을 보여주는거야 - 난 그저 폴더 자체를 원해요 :) – SoulieBaby

+0

아, 내가 당신을 위해 그것을 고쳤다 :) 당신이 그것을 표시 해야하는 경우. 및 .. $ scan = scandir 뒤에 다음 줄을 추가하십시오. if ($ subdir == 0) { echo ""; } – abelito

+0

다시 고마워하지만 지금은 아무 것도 보이지 않습니다. ( – SoulieBaby

6
//Requires PHP 5.3 
$it = new RecursiveTreeIterator(
    new RecursiveDirectoryIterator($dir)); 

foreach ($it as $k => $v) { 
    echo "<option>".htmlspecialchars($v)."</option>\n"; 
} 

당신은 RecursiveTreeIterator::setPrefixPart와 접두사를 사용자 정의 할 수 있습니다 .. 내가 파일, 폴더 만하지 않으려는 말을 잊어 버렸습니다.

0

당신은 PHP "글로브"기능 http://php.net/manual/en/function.glob.php을 사용하고, 재귀 함수 무한 레벨 깊이 갈 (자신을 호출하는 기능)을 구축 할 수 있습니다. 그것은

function glob_dir_recursive($dirs, $depth=0) { 
    foreach ($dirs as $item) { 
     echo '<option>' . str_repeat('-',$depth*1) . basename($item) . '</option>'; //can use also "basename($item)" or "realpath($item)" 
     $subdir = glob($item . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR); //use DIRECTORY_SEPARATOR to be OS independent 
     if (!empty($subdir)) { //if subdir array is not empty make function recursive 
      glob_dir_recursive($subdir, $depth+1); //execute the function again with current subdir, increment depth 
     } 
    } 
} 

사용 "을 위해 scandir"을 사용하여보다 짧은입니다 :

$dirs = array('galleries'); //relative path examples: 'galleries' or '../galleries' or 'galleries/subfolder'. 
//$dirs = array($_SERVER['DOCUMENT_ROOT'].'/galleries'); //absolute path example 
//$dirs = array('galleries', $_SERVER['DOCUMENT_ROOT'].'/logs'); //multiple paths example 

echo '<select>'; 
glob_dir_recursive($dirs); //to list directories and files 
echo '</select>'; 

이 정확히 요청 된 출력 유형을 생성 할 것이다.

관련 문제