simplecontent

PHP Snippets und Beispiele

Inhaltsverzeichnis A-Z Register
Beispiel ausführen

Verzeichnisgröße

Dateigrößen innerhalb eines Verzeichnis summieren

Das PHP Code Beispiel:
<?php


  
function ByteSize($bytes
   {

    if (!
is_int($bytes) || $bytes 0){
        return 
false;
    }

    
$map = array(
     
'GB' => array(10737418241),
     
'MB' => array(10485762),
     
'KB' => array(10242),
     
'Bytes' => array(10),
    );
   
    foreach(
$map as $k => $v){
        if (
$bytes >= $v[0]){
            break;
        }
    }
    
    
$f number_format($bytes $v[0], $v[1],',','.');

    if (
$bytes 2){
        
$k 'Byte';
    }
    
    return 
sprintf ('%s %s',$f$k);
  }

function 
dirinfo($path)
{
    
$tmp glob_recursive($path);
    
$qty_all count($tmp);
    
$tmp array_filter($tmp'is_file');
    
$qty_file count($tmp);
    
$qty_dir $qty_all-$qty_file;
    
$s array_map('filesize'$tmp);
    
$size ByteSizearray_sum($s) );

    
$arr = array(
        
0            => $qty_file,
        
1             => $qty_dir,
        
2             => $size,

        
'files'       => $qty_file,
        
'directories' => $qty_dir,
        
'size'           => $size,
    );

    return 
$arr;
}  
  
function 
glob_recursive($sDir$sPattern='*'$nFlags NULL)
{
  
$sDir escapeshellcmd($sDir);

  
// Get the list of all matching files currently in the
  // directory.

  
$aFiles glob("$sDir/$sPattern"$nFlags);

  
// Then get a list of all directories in this directory, and
  // run ourselves on the resulting array.  This is the
  // recursion step, which will not execute if there are no
  // directories.

  
foreach (glob("$sDir/*"GLOB_ONLYDIR) as $sSubDir)
  {
   
$aSubFiles glob_recursive($sSubDir$sPattern$nFlags);
   
$aFiles array_merge($aFiles$aSubFiles);
  }

  
// The array we return contains the files we found, and the
  // files all of our children found.

  
return $aFiles;
}

echo 
'<pre>';
var_dump(dirinfo(dirname(__FILE__)));
echo 
'</pre>';

?>

Dieses PHP Snippet soll in erster Linie als Beispiel und Anregung für eigene Bemühungen dienen.
Gerne darf man es für Projekte aller Art benutzen.
Möchte jemand das Snippet also solches in ähnlicher oder anderer Form veröffentlichen ist ein kleiner Hinweis auf simplecontent.net nicht zuviel verlangt oder Herr Koch?
Für die Abwesenheit von Fehlern kann natürlich keine Gewähr gegeben werden.