2010-11-17 140 views
2

我有以下功能,當我嘗試並返回值時,它只顯示1個文件夾,但當我回顯功能時,它顯示正確的信息。返回功能不工作,但回聲在PHP中工作

PHP代碼:

$FolderList = ""; 

function ListFolder($path) { 
    $path = str_replace("//","/",$path); 

    //using the opendir function 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 

    //Leave only the lastest folder name 
    $dirname = end(explode("/", $path)); 

    //display the target folder. 
    $FolderList .= ('<option value="">'.$path.'</option>'); 

    while(false !== ($file = readdir($dir_handle))) { 
     if($file!="." && $file!="..") { 
      if(is_dir($path."/".$file)) { 
       //Display a list of sub folders. 
       ListFolder($path."/".$file); 
      } 
     } 
    } 

    //closing the directory 
    closedir($dir_handle); 

    return $FolderList; //ERROR: Only Shows 1 Folder 
    echo $FolderList; //WORKS: Show All The Folders Correctly 
} 

感謝

+1

我相信這是與範圍的分辨率做的,但我的大腦不能正常工作不夠好,現在就看着辦吧......你定義的函數和我以外$ FolderList相信你會得到不可靠的結果,除非你將變量定義爲靜態和/或全局。 – Patrick 2010-11-17 15:12:45

+0

鏈接的帖子:http://stackoverflow.com/questions/4204728/how-to-display-folders-and-sub-folders-from-dir-in-php? – 2010-11-17 15:13:08

+0

我在函數內部試了一下,發生了一些事情 – Rickstar 2010-11-17 15:17:46

回答

2

給這一個鏡頭:

function ListFolder($path) 
{ 
    $path = str_replace("//","/",$path); 

    //using the opendir function 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 

    //Leave only the lastest folder name 
    $dirname = end(explode("/", $path)); 
    //display the target folder. 
    $FolderList = ('<option value="">'.$path.'</option>'); 
    while (false !== ($file = readdir($dir_handle))) 
    { 
     if($file!="." && $file!="..") 
     { 
      if (is_dir($path."/".$file)) 
      { 
       //Display a list of sub folders. 
       $FolderList .= ListFolder($path."/".$file); 
      } 
     } 
    } 


    //closing the directory 
    closedir($dir_handle); 

    return $FolderList; 
} 

echo ListFolder('/path/to/folder/'); 

我只是改變了$FolderList被分配到ListFolder函數的返回值。

1

每個函數調用都有自己的variable scope。您需要工會從遞歸調用返回的值與你收集的while循環的一個:

while(false !== ($file = readdir($dir_handle))) { 
    if($file!="." && $file!="..") { 
     if(is_dir($path."/".$file)) { 
      $FolderList .= ListFolder($path."/".$file); 
     } 
    } 
} 
2

內部while循環,你再打電話給ListFolder。這是可以做的,但是你不會在任何地方存儲結果,只是在每次調用ListFolder時回顯結果。

您在頁面上看到的正確格式不是最後回顯1個字符串。每次調用ListFolder時,它的單個目錄都會被回顯。

下面是可用的代碼。

function ListFolder($path) 
{ 

    $FolderList = ""; 
    $path = str_replace("//","/",$path); 

    //using the opendir function 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 

    //Leave only the lastest folder name 
    $dirname = end(explode("/", $path)); 
    //display the target folder. 

    $FolderList .= ('<option value="">'.$path.'</option>'); 

    while (false !== ($file = readdir($dir_handle))) 
    { 
     if($file!="." && $file!="..") 
     { 
      if (is_dir($path."/".$file)) 
      { 
       //Display a list of sub folders. 
       $FolderList .= ListFolder($path."/".$file); 
      } 
     } 
    } 

    //closing the directory 
    closedir($dir_handle); 

    return $FolderList; 
} 
1

你忘了捕捉函數的返回值調用

$FolderList .= ListFolder($path."/".$file); 

你只是一個文件夾添加到字符串,比調用函數,卻無可奈何與返回值。然後你返回$ FolderList,它只包含在while循環之前添加的一個條目

當* echo *它時,它只是直接發送到瀏覽器,而不管你是遞歸的哪個級別,所以你認爲,即$ FolderList已滿,但實際上它只是每個遞歸步驟中的每個步驟$ FolderList

0

替代方法

function ListFolder($path, &$FolderList = array()) { 
    $path = str_replace("//","/",$path); 

    //using the opendir function 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 

    //Leave only the lastest folder name 
    $dirname = end(explode("/", $path)); 

    //display the target folder. 
    $FolderList[] = '<option value="">'.$path.'</option>'; 

    while(false !== ($file = readdir($dir_handle))) { 
     if($file!="." && $file!="..") { 
      if(is_dir($path."/".$file)) { 
       //Display a list of sub folders. 
       ListFolder($path."/".$file, $FolderList); 
      } 
     } 
    } 

    //closing the directory 
    closedir($dir_handle); 

    return $FolderList; 
} 

$paths = ListFolder(getcwd()); 
echo "<select>".implode("", $paths)."</select>";