| 
| Another PHP Help Thread (Getting size of folders) |  
| Author: | Message: |  
| Ash_ Senior Member
 
     
 
  
 Posts: 638
 Reputation: 31
 36 /
  / – 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;
 }
 ?>
 
 | 
 |  
| 01-31-2005 06:32 AM |  |  
|  |  
| WDZ Former Admin
 
      
 
  
 Posts: 7105
 Reputation: 107
 – /
  /  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.   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 |  |  
|  |  
| Ash_ Senior Member
 
     
 
  
 Posts: 638
 Reputation: 31
 36 /
  / – 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 = " <img src='view/images/dossier.gif'>Folders<br><hr>";
 $temp2 = "<table width='100%' border=0><tr><td nowrap width='70%' class = 'text1'> <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 = "      ";
 
 $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'> ";
 $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-,
  Attachment: cssboxes.func.php (1.37 KB) This file has been downloaded 149 time(s).
 
 | 
 |  
| 01-31-2005 06:45 AM |  |  
|  |  
| -dt- Scripting Contest Winner
 
      
 
  ;o
 
 Posts: 1818
 Reputation: 74
 37 /
  /  Joined: Mar 2004
 
 | | RE: Another PHP Help Thread (Getting size of folders) lol just release my badly coded cssboxes function to the world   This post was edited on 01-31-2005 at 06:49 AM by -dt-.
 ![[Image: dt2.0v2.png]](http://thedt.net/img/dt2.0v2.png) Happy Birthday, WDZ | 
 |  
| 01-31-2005 06:48 AM |  |  
|  |  
| WDZ Former Admin
 
      
 
  
 Posts: 7105
 Reputation: 107
 – /
  /  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.    
BTW, see my edit above. | 
 |  
| 01-31-2005 06:48 AM |  |  
|  |  
| L. Coyote Senior Member
 
     
 
  Captain Obvious
 
 Posts: 977
 Reputation: 49
 40 /
  /  Joined: Aug 2004
 Status: Away
 
 | | RE: Another PHP Help Thread (Getting size of folders) Basically I just took off truesize() (it doesn't exist    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    code:Edit:<?
 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;
 ?>
 
  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.  This post was edited on 01-31-2005 at 06:59 AM by L. Coyote.
 
Hack, hack, hack!Finally became a Systems Analyst!
   | 
 |  
| 01-31-2005 06:49 AM |  |  
|  |  
| WDZ Former Admin
 
      
 
  
 Posts: 7105
 Reputation: 107
 – /
  /  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.   | 
 |  
| 01-31-2005 06:56 AM |  |  
|  |  
| Ash_ Senior Member
 
     
 
  
 Posts: 638
 Reputation: 31
 36 /
  / – 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?
 
 | 
 |  
| 01-31-2005 07:12 AM |  |  
|  |  
|  |  |