Recursive Directory Size Function
Get the filesize of a directory and all files and subdirectories.
This PHP function scans a given directory and gets the filesize of all files it finds
in the specified directory. It calls itself with all subdirectories to get the total size of the folder.
By default it returns the filesize of the given directory in bytes and without any unit. If you specify that you want
a human readable formatted output, this function returns the total filesize rounded in MB, KB or bytes.
Keep an eye on your disquota, supervise upload folders and more with the
recursive directory filesize function:

download Recursive Directory Size Function with comments
01 <?php
02
03 // ------------ lixlpixel recursive PHP functions -------------
04 // recursive_directory_size( directory, human readable format )
05 // expects path to directory and optional TRUE / FALSE
06 // PHP has to have the rights to read the directory you specify
07 // and all files and folders inside the directory to count size
08 // if you choose to get human readable format,
09 // the function returns the filesize in bytes, KB and MB
10 // ------------------------------------------------------------
11
12 // to use this function to get the filesize in bytes, write:
13 // recursive_directory_size('path/to/directory/to/count');
14
15 // to use this function to get the size in a nice format, write:
16 // recursive_directory_size('path/to/directory/to/count',TRUE);
17
18 function recursive_directory_size($directory, $format=FALSE)
19 {
20 $size = 0;
21
22 // if the path has a slash at the end we remove it here
23 if(substr($directory,-1) == '/')
24 {
25 $directory = substr($directory,0,-1);
26 }
27
28 // if the path is not valid or is not a directory ...
29 if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory))
30 {
31 // ... we return -1 and exit the function
32 return -1;
33 }
34 // we open the directory
35 if($handle = opendir($directory))
36 {
37 // and scan through the items inside
38 while(($file = readdir($handle)) !== false)
39 {
40 // we build the new path
41 $path = $directory.'/'.$file;
42
43 // if the filepointer is not the current directory
44 // or the parent directory
45 if($file != '.' && $file != '..')
46 {
47 // if the new path is a file
48 if(is_file($path))
49 {
50 // we add the filesize to the total size
51 $size += filesize($path);
52
53 // if the new path is a directory
54 }elseif(is_dir($path))
55 {
56 // we call this function with the new path
57 $handlesize = recursive_directory_size($path);
58
59 // if the function returns more than zero
60 if($handlesize >= 0)
61 {
62 // we add the result to the total size
63 $size += $handlesize;
64
65 // else we return -1 and exit the function
66 }else{
67 return -1;
68 }
69 }
70 }
71 }
72 // close the directory
73 closedir($handle);
74 }
75 // if the format is set to human readable
76 if($format == TRUE)
77 {
78 // if the total size is bigger than 1 MB
79 if($size / 1048576 > 1)
80 {
81 return round($size / 1048576, 1).' MB';
82
83 // if the total size is bigger than 1 KB
84 }elseif($size / 1024 > 1)
85 {
86 return round($size / 1024, 1).' KB';
87
88 // else return the filesize in bytes
89 }else{
90 return round($size, 1).' bytes';
91 }
92 }else{
93 // return the total filesize in bytes
94 return $size;
95 }
96 }
97 // ------------------------------------------------------------
98
99 ?>

recursive directory size function without comments:
top of page
