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