当我尝试返回vaule时,我有以下功能,它只显示1个文件夹,但当我回显该功能时,它显示正确的信息。
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
}
由于
答案 0 :(得分: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 :(得分: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
函数的返回值。
答案 2 :(得分:1)
每个函数调用都有自己的variable scope。您需要将递归调用的返回值与您在while
循环中收集的值结合起来:
while(false !== ($file = readdir($dir_handle))) {
if($file!="." && $file!="..") {
if(is_dir($path."/".$file)) {
$FolderList .= ListFolder($path."/".$file);
}
}
}
答案 3 :(得分:1)
您忘记捕获函数调用的返回值
$FolderList .= ListFolder($path."/".$file);
您只需向字符串添加一个文件夹,而不是调用该函数,但不对返回值执行任何操作。然后返回 $ FolderList ,它只包含你在while循环之前添加的一个条目
当* echo *它时,它只是直接发送到浏览器的递归级别,所以你想, $ FolderList 已满,但实际上它只是每个递归步骤中的每个文件 $ FolderList 。
答案 4 :(得分: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>";