Directory Tree

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Directory Tree

Post by thatsme »

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

Code: Select all

 
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&#058;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&#058;go('" . $currentfile . "')\">". substr($currentfile, strrpos($currentfile, '/')) . "</a><br>";
 
echo $last_dir;
//$d[] = $last_dir;
}
}
}
 
// return $d;
}
 
ls_recursive2("../"));
//print_r(ls_recursive2("../"));
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Directory Tree

Post by jaoudestudios »

You have it there in your code.

Instead of echo, use return. You can return an array.
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Directory Tree

Post by thatsme »

yes, i commented echo and uncommented $d[] and also uncommented the return $d. print_r(ls_recursive2("../")); is not printing all the contents.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Directory Tree

Post by jaoudestudios »

you can return an array but you can not echo an array.
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Directory Tree

Post by thatsme »

changed the code, I am not getting all the contents.

Code: Select all

 
<?php
 
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 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
      }
               
      if (is_dir($currentfile))
      {
        if ($file != '.' && $file != '..')
      {
        $last_dir .= "<img src='images/folder.gif' alt='' align='middle' width='16' height='16' border='0'>&nbsp;<a href=\"javascript&#058;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'>&nbsp;<a href=\"javascript&#058;go('" . $currentfile . "')\">". substr($currentfile, strrpos($currentfile, '/')) . "</a><br>";
        //echo $last_dir; 
        $d[] = $last_dir;
        //echo $last_dir;
 
      }
    }
  }
  
 return $d;
}
 
 
print_r(ls_recursive2("../"));
 
?>
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Directory Tree

Post by jaoudestudios »

Which part are you missing?

Can you post what you expect and what you get?
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Directory Tree

Post by thatsme »

i have a folder 'a' which has folder 'aa' aa folder has 'aaa'. if i use echo, i am getting the output as,

a
aa
aaa

the above is correct.

If i store in array and return it i am getting the output,
Array ( [0] => a
)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Directory Tree

Post by jaoudestudios »

An array might be the better way of doing it, but for now, save it as a string.

Use this...

Code: Select all

 
$d .= $last_dir.",";
 
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: Directory Tree

Post by thatsme »

again changed to, I am getting the output as

a,

Code: Select all

 
 if (is_dir($currentfile))
      {
        if ($file != '.' && $file != '..')
      {
        $last_dir .= "<img src='images/folder.gif' alt='' align='middle' width='16' height='16' border='0'>&nbsp;<a href=\"javascript&#058;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'>&nbsp;<a href=\"javascript&#058;go('" . $currentfile . "')\">". substr($currentfile, strrpos($currentfile, '/')) . "</a><br>";
        $d .= $last_dir.","; 
        //$d[] = $last_dir;
        //echo $last_dir;
 
      }
    }
  }
 
Post Reply