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
thatsme
Forum Commoner
Posts: 87 Joined: Sat Apr 07, 2007 2:18 am
Post
by thatsme » Wed Jan 14, 2009 3:22 am
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: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("../"));
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Wed Jan 14, 2009 3:33 am
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
Post
by thatsme » Wed Jan 14, 2009 3:59 am
yes, i commented echo and uncommented $d[] and also uncommented the return $d. print_r(ls_recursive2("../")); is not printing all the contents.
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Wed Jan 14, 2009 5:05 am
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
Post
by thatsme » Wed Jan 14, 2009 6:21 am
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 .= " ";
}
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;
//echo $last_dir;
}
}
}
return $d;
}
print_r(ls_recursive2("../"));
?>
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Wed Jan 14, 2009 6:23 am
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
Post
by thatsme » Wed Jan 14, 2009 6:46 am
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
)
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Wed Jan 14, 2009 6:52 am
An array might be the better way of doing it, but for now, save it as a string.
Use this...
thatsme
Forum Commoner
Posts: 87 Joined: Sat Apr 07, 2007 2:18 am
Post
by thatsme » Wed Jan 14, 2009 6:55 am
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'> <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>";
$d .= $last_dir.",";
//$d[] = $last_dir;
//echo $last_dir;
}
}
}