Forum Moderators: coopster

Message Too Old, No Replies

show all filesizes

my code doesn't work

         

WhosAWhata

3:27 am on Jun 1, 2004 (gmt 0)

10+ Year Member



why doesn't this code work? it returns a blank file

[pre][quote]<?
$html = "<table width=80% cols=2>";
function display($dir) {
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(is_dir($f)) {
$ds = $dir.$f."/";
display($ds);
}else{
$html .= "<tr><td>".$dir.$f."</td><td>".filesize($dir.$f)."</td></tr>";
}
}
}
}
display("./");
echo $html;
?>[/quote][/pre]

dcrombie

3:10 pm on Jun 1, 2004 (gmt 0)



I could be wrong, but isn't the current directory "." instead of "./"?

jatar_k

4:03 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



keep the ./ but I tried it and changed it to this

<? 
function display($dir) {
global $html;
$html = "<table width=80% cols=2>";
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(is_dir($f)) {
$ds = $dir.$f."/";
display($ds);
}else{
$html .= "<tr><td>".$dir.$f."</td><td>".filesize($dir.$f)."</td></tr>";
}
}
}
}
display("./");
echo $html;
?>

note the global var, I changed it to echo inside the function which told me it was just a scope issue.

WhosAWhata

9:32 pm on Jun 1, 2004 (gmt 0)

10+ Year Member



thanks, but it returned this
<table width=80% cols=2><tr><td>./aaaaa/index.php</td> <td>294</td></tr><tr><td>./math.php</td> <td>58</td></tr><tr><td>./cookiepass.php</td> <td>449</td></tr><tr><td>./func.php</td><td>113</td> </tr><tr><td>./size.php</td><td>337</td></tr>

as you might guess i have way more than 5 files on my server

any thoughts on why it stopped?
does it work for you?

[edited by: jatar_k at 9:43 pm (utc) on June 1, 2004]
[edit reason] fixed sidescroll [/edit]

jatar_k

9:48 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about this?

<? 
function display($dir) {
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(is_dir($f)) {
$ds = $dir.$f."/";
$innerstuff = display($ds);
$html .= $innerstuff;
}else{
$html .= "<tr><td>".$dir.$f."</td><td>".filesize($dir.$f)."</td></tr>\n";
}
}
}
return $html;
}
echo "<table width=80% cols=2>\n";
$stuff = display("./");
echo $stuff;
echo "</table>\n";
?>

WhosAWhata

1:25 am on Jun 2, 2004 (gmt 0)

10+ Year Member



you got it
thanks a million
one more question?
how would i go about sorting it by size? i think i would use natsort() but i'm not really sure where to implement it

jatar_k

1:37 am on Jun 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



then you would need to put them into an array

instead of
$html .= "<tr><td>".$dir.$f."</td><td>".filesize($dir.$f)."</td></tr>\n";

then
$myarr[] = array($dir.$f,filesize($dir.$f));

then you would need to sort them when completed and then have a loop to convert to html and return

I think for the multidimensional array you could use array_multisort [ca.php.net] but if you make the filename the key them you can use natsort. My code would be wrong then though.

WhosAWhata

2:15 am on Jun 2, 2004 (gmt 0)

10+ Year Member



thanks, i figured as much
i'll play with it