Categories: PHP

Calculate the size, count files & folders of a directory with PHP

Here a PHP Class that calculates the size, number of files & folders of a specific directory.

< ?php 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);
}
}
?>

 

Save this bit of code in a file called: directory.class.php

How to use

< ?php include ‘directory.class.php’; /* Path to Directory – IMPORTANT: with ‘/’ at the end */ $directory = ‘/home/mywebsite.com/public_html/’; /* Calculate size in: B (Bytes), KB (Kilobytes), MB (Megabytes), GB (Gigabytes) */ $size_in = ‘MB’; /* Number of decimals to show */ $decimals = 2; $directory_size = new Directory_Calculator; /* Initialize Class */ $directory_size->size_in = $size_in;
$directory_size->decimals = $decimals;

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

echo “The directory “.$directory.” has a size of “.$array[‘size’].” “.$size_in.”, “.$array[‘files’].” files & “.$array[‘folders’].” folders.”;
?>

 

SGH

At the same time, I can’t think of anything more satisfying than sitting in front of TV, eating take-away, lights down low, in my pyjamas and watching crap on TV. Basically, I strive to live a balanced life. Fun. Serious. Fast. Slow. Brainy. Bimbotic. Intense. Lazy. You get the drift.

Recent Posts

Free GG Mug Mockup

GG Mug Mockup designed for logo branding of companies and brands.

6 years ago

Lovtony Font Duo – Modern Sans Serif

A great font/script for creating beautiful logos, posters, wedding invitations, blog posts, social media, and…

6 years ago

Free Men T-Shirt Mockup

Realistic T-Shirt Mockup PSD which you can use freely to showcase your apparel design.

6 years ago

Classic Coffee Mug Mock-up Free PSD

Classic coffee mug Mock-up Free PSD. Free mug mockup PSD to showcase your branding design…

6 years ago

Juliette – Stylish Handwritten Signature Font

Juliette - Stylish Handwritten Signature Font

6 years ago

XXII Aven Sans Serif Font Family

XXII Aven Font Family - a new modern all-caps sans serif font with versatility in creativity.

6 years ago