What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Another PHP Help Thread (Getting size of folders)

Another PHP Help Thread (Getting size of folders)
Author: Message:
Ash_
Senior Member
****

Avatar

Posts: 638
Reputation: 31
35 / Male / –
Joined: Aug 2004
O.P. Another PHP Help Thread (Getting size of folders)
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;
}
?>
[Image: jeansiger5.jpg]
01-31-2005 06:32 AM
Profile PM Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: Another PHP Help Thread (Getting size of folders)
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.

This post was edited on 01-31-2005 at 06:46 AM by WDZ.
01-31-2005 06:41 AM
Profile PM Web Find Quote Report
Ash_
Senior Member
****

Avatar

Posts: 638
Reputation: 31
35 / Male / –
Joined: Aug 2004
O.P. RE: Another PHP Help Thread (Getting size of folders)
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-,

.php File Attachment: cssboxes.func.php (1.37 KB)
This file has been downloaded 110 time(s).
[Image: jeansiger5.jpg]
01-31-2005 06:45 AM
Profile PM Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: Another PHP Help Thread (Getting size of folders)
lol just release my badly coded cssboxes function to the world :P


This post was edited on 01-31-2005 at 06:49 AM by -dt-.
[Image: dt2.0v2.png]      Happy Birthday, WDZ
01-31-2005 06:48 AM
Profile PM Web Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: Another PHP Help Thread (Getting size of folders)
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.
01-31-2005 06:48 AM
Profile PM Web Find Quote Report
L. Coyote
Senior Member
****

Avatar
Captain Obvious

Posts: 981
Reputation: 49
38 / Male / Flag
Joined: Aug 2004
Status: Away
RE: Another PHP Help Thread (Getting size of folders)
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

This post was edited on 01-31-2005 at 06:59 AM by L. Coyote.

Hack, hack, hack!
Finally became a Systems Analyst! :spam:

01-31-2005 06:49 AM
Profile PM Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: Another PHP Help Thread (Getting size of folders)
I also got the function working fine after the changes I posted above. :spam:
01-31-2005 06:56 AM
Profile PM Web Find Quote Report
Ash_
Senior Member
****

Avatar

Posts: 638
Reputation: 31
35 / Male / –
Joined: Aug 2004
O.P. RE: Another PHP Help Thread (Getting size of folders)
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?
[Image: jeansiger5.jpg]
01-31-2005 07:12 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On