communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "directories" - download "human readable size" - open demo of "human readable size"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

directories - human readable size was created/last modified on Fri 19 Mar 2004 9:50 PM

to give a user the size of his account (or the admin the size of the users folder) in a more human readable format, you can use this

it will return the value in megabytes so its more easy to read.
       
      <?
      $varray
= explode("&", $_SERVER['QUERY_STRING']);
      for(
$x=0; $x<=count($var); $x++)
      {
       
$var = explode("=", $varray[$x]);
       $
$var[0] = $var[1];
      }
      
      function
dirsize($directory)
        {
         if (!
is_dir($directory)) return -1;
         
$size = 0;
         if (
$DIR = opendir($directory))
         {
          while ((
$dirfile = readdir($DIR)) !== false)
          {
             if (
is_link($directory . '/' . $dirfile) || $dirfile == '.' || $dirfile == '..')
              continue;
             if (
is_file($directory . '/' . $dirfile))
              
$size += filesize($directory . '/' . $dirfile);
             else if (
is_dir($directory . '/' . $dirfile))
             {
              
$dirSize = dirsize($directory . '/' . $dirfile);
              if (
$dirSize >= 0) $size += $dirSize;
              else return -
1;
             }
          }
          
closedir($DIR);
         }
         return
$size;
        }
        
// stolen from another fine user on php.net
        //
        
function format_size($rawSize)
        {
         if (
$rawSize / 1048576 > 1)
          return
round($rawSize/1048576, 1) . 'MB';
         else if (
$rawSize / 1024 > 1)
          return
round($rawSize/1024, 1) . 'KB';
         else
          return
round($rawSize, 1) . 'bytes';
        }
      
      
$dir = '/phpsnippets';
      
$size = dirsize($DOCUMENT_ROOT.$dir);
      if (
$size < 0){
       echo
'<p>ERROR! Bad path "'.$dir.'" !';
      }else{
       echo
'<p>the '.$dir.' folder is ' . format_size($size);
      }
      
      
?>
    
             

learn more about some of the PHP functions used in this snippet: array, closedir, continue, count, dir, echo, else, end, explode, file, filesize, for, function, is_dir, is_file, is_link, link, min, opendir, readdir, return, round, var, while

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari