PHP Code and PHP Class to Calculate Files In a Directory and File Size of a Directory or folder

PHP Code and PHP Class to Calculate Files In a Directory and File Size of a Directory or folder

size_in = $size_in;
$directory_size->decimals = $decimals;

// return an array with: size, total files & folders
$array = $directory_size->size($directory);

echo "The directory ".$directory." has a size of
".$array['size']." ".$size_in.", ".$array['files']." files &
".$array['folders']." folders.";
?>



Now save this file as calculate.directory.class.php

http://vaseemansari.blogspot.com/search/label/PHP
// calculate-the-size-number-of-files-folders-of-a-directory.html


class Directory_Calculator {
var $size_in;
var $decimals;
function calculate_whole_directory($directory)
{
if($handle = OPENDIR($directory))
{
$size = 0;
$folders = 0;
$files = 0;

while (FALSE !== ($file = READDIR($handle)))
{
if ($file != "." && $file != "..")
{
if(is_dir($directory.$file))
{
$array = $this->calculate_whole_directory($directory.$file.'/');
$size += $array['size'];
$files += $array['files'];
$folders += $array['folders'];
}
else
{
$size += FILESIZE($directory.$file);
$files++;
}
}
}
closedir($handle);
}

$folders++;

return array('size' => $size, 'files' => $files, 'folders' => $folders);
}
function size_calculator($size_in_bytes)
{
if($this->size_in == 'B')
{
$size = $size_in_bytes;
}
elseif($this->size_in == 'KB')
{
$size = (($size_in_bytes / 1024));
}
elseif($this->size_in == 'MB')
{
$size = (($size_in_bytes / 1024) / 1024);
}
elseif($this->size_in == 'GB')
{
$size = (($size_in_bytes / 1024) / 1024) / 1024;
}
$size = round($size, $this->decimals);
return $size;
}

function size($directory)
{
$array = $this->calculate_whole_directory($directory);
$bytes = $array['size'];
$size = $this->size_calculator($bytes);
$files = $array['files'];
$folders = $array['folders'] - 1; // exclude the main folder

return array('size' => $size, 'files' => $files, 'folders' => $folders);
}
}
?>

Simply run the file example.php and you will get the information about no of files and size of that folder.


Tags: Funny Pictures, Pakistani Girls Pictures Indian Teen Girls, Sports Celebrities Pictures, Hollywood Celebrities, Bollywood Celebrities, Katrina Kaif, South Indian Actress, Hot Teen Models

No comments:

Post a Comment