我有以下功能,当我尝试并返回值时,它只显示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
}
感谢
我相信这是与范围的分辨率做的,但我的大脑不能正常工作不够好,现在就看着办吧......你定义的函数和我以外$ FolderList相信你会得到不可靠的结果,除非你将变量定义为静态和/或全局。 – Patrick 2010-11-17 15:12:45
链接的帖子:http://stackoverflow.com/questions/4204728/how-to-display-folders-and-sub-folders-from-dir-in-php? – 2010-11-17 15:13:08
我在函数内部试了一下,发生了一些事情 – Rickstar 2010-11-17 15:17:46