2010-11-17 113 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>";