useful recursive PHP functions

recursively ...

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 ?>

learn more about the PHP functions used in this script:

substr( ), file_exists( ), is_dir( ), is_readable( ), opendir( ), readdir( ), is_file( ), filesize( ), closedir( ) and round( ).

recursive directory size function without comments:

download Recursive Directory Size Function

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 // ------------------------------------------------------------
07 function recursive_directory_size($directory, $format=FALSE)
08 {
09     $size = 0;
10     if(substr($directory,-1) == '/')
11     {
12         $directory = substr($directory,0,-1);
13     }
14     if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory))
15     {
16         return -1;
17     }
18     if($handle = opendir($directory))
19     {
20         while(($file = readdir($handle)) !== false)
21         {
22             $path = $directory.'/'.$file;
23             if($file != '.' && $file != '..')
24             {
25                 if(is_file($path))
26                 {
27                     $size += filesize($path);
28                 }elseif(is_dir($path))
29                 {
30                     $handlesize = recursive_directory_size($path);
31                     if($handlesize >= 0)
32                     {
33                         $size += $handlesize;
34                     }else{
35                         return -1;
36                     }
37                 }
38             }
39         }
40         closedir($handle);
41     }
42     if($format == TRUE)
43     {
44         if($size / 1048576 > 1)
45         {
46             return round($size / 1048576, 1).' MB';
47         }elseif($size / 1024 > 1)
48         {
49             return round($size / 1024, 1).' KB';
50         }else{
51             return round($size, 1).' bytes';
52         }
53     }else{
54         return $size;
55     }
56 }
57 // ------------------------------------------------------------
58
59 ?>

top of page

© 2017 lixlpixel