The below function works fine when i echo. I needed the function to return the contents as i am using template to echo the contents. When i tried to store the contents in an array $d (as i have done in the below code and commented) all the contents are not listed. Please have a look at the code and tell me how to return the contents and also how to display the returned array. Thanks function ls_recursive2($dir) { if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) { $currentfile = $dir . "/" . $file; $last_dir = ""; // Calculate they identation. $count = substr_count($currentfile, '/'); $minus_count = substr_count($_SERVER['DOCUMENT_ROOT'], '/'); $count -= ($minus_count + 2); for($p = 0; $p < $count; $p++) { $last_dir .= " "; } if (is_dir($currentfile)) { if ($file != '.' && $file != '..') { $last_dir .= "<img src='images/folder.gif' alt='' align='middle' width='16' height='16' border='0'> <a href=\"javascript:go('" . $currentfile . "')\">". substr($currentfile, strrpos($currentfile, '/')) . "</a><br>"; echo $last_dir; //$d[] = $last_dir; ls_recursive2($currentfile); } } else { $last_dir .= "<img src='images/file.gif' alt='' align='middle' width='16' height='16' border='0'> <a href=\"javascript:go('" . $currentfile . "')\">". substr($currentfile, strrpos($currentfile, '/')) . "</a><br>"; echo $last_dir; //$d[] = $last_dir; } } } // return $d; } ls_recursive2("../")); //print_r(ls_recursive2("../"));