Shoutbox

Another PHP Help Thread (Getting size of folders) - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Another PHP Help Thread (Getting size of folders) (/showthread.php?tid=37735)

Another PHP Help Thread (Getting size of folders) by Ash_ on 01-31-2005 at 06:32 AM

ok, i been mucking around with php for a while for school.
i've attempted to write a function that gets the size of folders.

can someone tell me whats wrong with it. i know calling the same function i am in is dodgy but i cant think of another way unless i copy the function again with a different name..

this current code doesent work ><

code:
<?
function foldersize ($folder){
$handle = opendir($folder);
if ($handle = opendir($folder)) {
   while (false !== ($file = readdir($handle))) {
    if($file !='.' && $file !='' && $file != '../'){
            if (is_dir($folder.'/'.$file)){
            $temp2 = $temp2 . "$folder/$file";
            $foldersize = $foldersize + foldersize("$folder/$file");
            }else{
            $temp = $temp . "<b>File</b>: $folder/$file<br>";
$foldersize = $foldersize + filesize("$folder/$file");
}
}
}
}
closedir($handle);
clearstatcache();
$foldersize = truesize($foldersize);
//echo $temp;
//echo "$temp2 <br>";
return $foldersize;
}
?>

RE: Another PHP Help Thread (Getting size of folders) by WDZ on 01-31-2005 at 06:41 AM

First of all, what's this?

$handle = opendir($folder);
if ($handle = opendir($folder)) {

Delete the first one.

Also, is truesize() a function you made yourself? I've never heard of it.

There might be other problems with the script... this is just my initial response. :p

Edit: Another problem: $file != '../' is wrong. The name would not have a trailing slash.


RE: Another PHP Help Thread (Getting size of folders) by Ash_ on 01-31-2005 at 06:45 AM

yes , it is a different function here ill upload the script.

code:
<?php
include("cssboxes.func.php");
$news = "| <b>C</b>urrent <b>N</b>ews | Plans for Assembler Trainer is proceding, Static offsets for Gold in Baldurs gate have been found.";
$Mtr = "./view/images/tr.png";
$Mtl = "./view/images/tl.png";
$Mbl = "./view/images/bl.png";
$Mbr = "./view/images/br.png";
$string = '';
$folders = '';
$temp = "&nbsp;<img src='view/images/dossier.gif'>Folders<br><hr>";
$temp2 = "<table width='100%' border=0><tr><td nowrap width='70%' class = 'text1'>&nbsp;<img src='view/images/script.gif'><font color='white'>File Name</font></td><td nowrap width='30%' class=' text1'><font color='White'>Size</font></td></tr></table><hr>";
$Mcol = "#178AFF";
$Str = "./view/images/tr.png";
$Stl = "./view/images/tl.png";
$Sbl = "./view/images/bl.png";
$Sbr = "./view/images/br.png";
$ck = "3dt";

$Scol = "#178AFF";
$textcol = "#fff";
$tab = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp";

$text = "<p><center><b>A</b>sh_'s <a href='index.php'><font color='White'>Web Server</font></a></center></p></marquee>";
cssbox ($text,400,10,200,"absolute",$Scol,$textcol,$Str,$Sbr,$Sbl,$Stl,"",1);

$page = 'index.php';
$folder = $_GET['fol'];
$main = './';
if ($folder == "")
{
$folder = $main;
}
if ($handle = opendir($folder)) {
   while (false !== ($file = readdir($handle))) {
    if($file !='.' && $file !=''){
        if (is_dir($folder.'/'.$file)){
            $foldersize =  foldersize($file);
            $temp = $temp ."<table width='100%' border=0><tr><td nowrap class='text1'>$tab</td>";
            $temp = $temp ."<td width='70%' class='text1'>";
            $temp = $temp ."<img src='view/images/dossier.gif' width='23' height='16' align='absbottom'>";
            $temp = $temp ."<a href='$page?fol=$folder/$file' class='text1'><font color='White'>$file</font></a>";
            $temp = $temp ."</td><td width='30%' class=' text1'>$foldersize</td></tr></table>";
        }
        else
        {
            $file_extension = strtolower(substr(strrchr($file, '.'), 1));
            $temp2 = $temp2 ."<table width='100%' border=0> <tr><td nowrap class='text1'>$tab</td>";
            $temp2 = $temp2 ."<td nowrap width='60%' class='text1'>";
            $temp2 = $temp2 ."<img src='view/images/script.gif' width='23' height='16' align='absbottom'>&nbsp;";
            $temp2 = $temp2 ."<a href='$folder/$file' class='text1'><font color='White'>$file</font></a>";
            $temp2 = $temp2 ."</td><td width='30%' class=' text1'>". truesize(filesize($main.$folder."/".$file))."</td>";
            if ($file_extension == "php" or $file_extension == "js" or $file_extension == "css"){
            $temp2 = $temp2 ."<td width='10%' class=' text1'> <a href=tester.php?file=$file&&noload=1><font color='White'>Edit</font></a></tr></table>";
            }
            }
    }





   }

   closedir($handle);
  clearstatcache();
}
cssbox ($temp,150,100,300,"absolute",$Scol,$textcol,$Str,$Sbr,$Sbl,$Stl,"",1);
cssbox ($temp2,550,100,300,"absolute",$Scol,$textcol,$Str,$Sbr,$Sbl,$Stl,"",1);
?>
<?
function foldersize ($folder){
$handle = opendir($folder);
if ($handle = opendir($folder)) {
   while (false !== ($file = readdir($handle))) {
    if($file !='.' && $file !='' && $file != '../'){
            if (is_dir($folder.'/'.$file)){
            //$temp2 = $temp2 . "$folder/$file";
            //echo foldersize("$folder/$file");
            }else{
            $temp = $temp . "<b>File</b>: $folder/$file<br>";
$foldersize = $foldersize + filesize("$folder/$file");
}
}
}
}
closedir($handle);
clearstatcache();
$foldersize = truesize($foldersize);
//echo $temp;
//echo "$temp2 <br>";
return $foldersize;
}
?>

<?
function truesize ($size){
$size = $size / 1024;
if ($size / 1024>1024){
return Round($size  /1024 /1024 , 2) . " GB";
}else{
     if ($size>1024){
        return  Round($size / 1024 , 2) . " MB";
        }else{
        if ($size<1024){
        return Round($size ,2). " KB";
        }
        }
        }
        }


    ?>


you'll also need teh css.box function coded by -dt-,
RE: Another PHP Help Thread (Getting size of folders) by -dt- on 01-31-2005 at 06:48 AM

lol just release my badly coded cssboxes function to the world :P


RE: Another PHP Help Thread (Getting size of folders) by WDZ on 01-31-2005 at 06:48 AM

quote:
Originally posted by Ash_
yes , it is a different function here ill upload the script.
That wasn't really necessary... I was only curious whether or not you called an undefined function. :p

BTW, see my edit above.
RE: Another PHP Help Thread (Getting size of folders) by L. Coyote on 01-31-2005 at 06:49 AM

Basically I just took off truesize() (it doesn't exist :s or you forgot to add it here), changed the quotes to my style (I mostly use single quotes). Not really a big change, imo. I did take off a / slash (it's commented).

I ran it in a big directory and gave me the correct bytes (Y)

code:
<?
function foldersize($folder){
if($handle = opendir($folder)) {
  while (false !== ($file = readdir($handle))) {
   if($file !='.' && $file !='' && $file != '..'){ // no / slash in $file != '../'
    if(is_dir($folder.'/'.$file)){
    $foldersize = $foldersize + foldersize($folder.'/'.$file);
    }else{
    $foldersize = $foldersize + filesize($folder.'/'.$file);
    }
   }
  }
}
closedir($handle);
clearstatcache();
return $foldersize;
}

$total = foldersize('projects');

echo $total;
?>

Edit: took off the $temps.

Btw, when I started to reply to this thread it didn't have replies, but the preview page slowed me down. :s
RE: Another PHP Help Thread (Getting size of folders) by WDZ on 01-31-2005 at 06:56 AM

I also got the function working fine after the changes I posted above. :spam:


RE: Another PHP Help Thread (Getting size of folders) by Ash_ on 01-31-2005 at 07:12 AM

it still doesent work it just doesent return and errors.

eg
i have a folder named "girl"

Girl/
1.png 24kb
index.php 1kb
2.png 36 kb
function -> tempexe.exe 360kb

returns 61kb.
so it doesent add the tempexe.exe size?