Rickstar
Rickstar

Reputation: 6189

return in function not working but echo works in PHP

i have the following function when i try and return the vaule its only shows 1 folder but when i echo the function it show the correct informaion.

PHP Code:

$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
}

Thanks

Upvotes: 2

Views: 3599

Answers (5)

KingCrunch
KingCrunch

Reputation: 131881

You forgot to catch the return value of the function calls

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

You just add one folder to the string, than call the function, but do nothing with the return value. Then you return $FolderList, which only contains the one entry you add before the while-loop

When *echo*ing it, its just send directly to the browser independently on which level of recursion you are, so you think, that $FolderList is full, but in fact its just every sungle $FolderList from each recursion step.

Upvotes: 1

Phill Pafford
Phill Pafford

Reputation: 85318

Alternative Method

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>";

Upvotes: 0

Jim
Jim

Reputation: 18853

Give this a shot:

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/');

I simply changed the $FolderList to be assigned to the return value of the ListFolder function.

Upvotes: 2

castis
castis

Reputation: 8223

Inside your while loop, you're calling ListFolder again. This is okay to do but you're not storing the result anywhere and just echoing the result every time ListFolder is called.

That correct format you're seeing on the page is not that 1 string being echoed at the end. its a single directory being echoed every time ListFolder is being called.

Below is the code that works.

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;
}

Upvotes: 2

Gumbo
Gumbo

Reputation: 655239

Each function call has its own variable scope. You need to union the returned value from your recursive call with the one that you’ve gathered in the while loop:

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

Upvotes: 1

Related Questions