Calculate dir size recursively with PHP (and count files)

This small PHP function lets you calculate the dir size entering each sub dir and making the sum of the…

February 1, 2010

This small PHP function lets you calculate the dir size entering each sub dir and making the sum of the filesize of every file contained. Returns an array of two values: size and numbers of file. The second function shows how format the size in a more readable way (with abbreviation MB, KB, GB).

function dirsize($dir) {
	if(is_file($dir)) return array('size'=>filesize($dir),'howmany'=>0);
	if($dh=opendir($dir)) {
		$size=0;
		$n = 0;
		while(($file=readdir($dh))!==false) {
			if($file=='.' || $file=='..') continue;
			$n++;
			$data = $this->dirsize($dir.'/'.$file);
			$size += $data['size'];
			$n += $data['howmany'];
		}
		closedir($dh);
		return array('size'=>$size,'howmany'=>$n);
	} 
	return array('size'=>0,'howmany'=>0);
}

If you want to show the file size in a more readable way you can use this second function to format the value:

function file_size($fsizebyte) {
	if ($fsizebyte < 1024) {
		$fsize = $fsizebyte." bytes";
	}elseif (($fsizebyte >= 1024) && ($fsizebyte < 1048576)) {
		$fsize = round(($fsizebyte/1024), 2);
		$fsize = $fsize." KB";
	}elseif (($fsizebyte >= 1048576) && ($fsizebyte < 1073741824)) {
		$fsize = round(($fsizebyte/1048576), 2);
		$fsize = $fsize." MB";
	}elseif ($fsizebyte >= 1073741824) {
		$fsize = round(($fsizebyte/1073741824), 2);
		$fsize = $fsize." GB";
	};
	return $fsize;
}

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “Calculate dir size recursively with PHP (and count files)”

3 thoughts

  1. Jos says:

    Grazie per lo script, il risultato del calcolo è veloce e comprensibile grazie alla funzione file_size

  2. wtf says:

    Do you even check your own code, or its even yours ?
    Fatal error: Using $this when not in object context on line 9

  3. Giulio Pons says:

    Sometimes I take pieces of code from my works and put them into posts, when I do this action late at night this brings some errors. Sorry. :) Can you fix the error on your own?

Comments are closed

Recommended

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

October 19, 2012

How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

October 8, 2012

ASP Function to count files in a folder

This ASP function counts files of a specified extension inside a folder: this is done by creating a file system…

February 9, 2010

A new avatar generator for WordPress with get_avatar and Flathash

Brand new avatars for WordPress Gravatar service

April 7, 2017

3 ways to add author’s image in WordPress

Add author's image in WordPress without a plugin

April 5, 2017

Orari trenord, corri solo quando ce n’è bisogno

Hai presente quando corri in stazione per prendere il treno al volo e, quando arrivi, ti accorgi che il treno ha 12 minuti di ritardo?

March 30, 2017