On a member-based site we operate we put a limit on the size of a directory before additional credit is required. The following function will recursively look through an entire directory and return a filesize. While the value returned is in bytes, you can alter it into a human readable string by using the first function from this article ("Return a Readable Filesize with PHP").
The PHP Function
1
<?php
2
/*
3
Return the Size of an Entire Directory with PHP
4
http://www.beliefmedia.com/return-size-directory-php
5
*/
6
7
function beliefmedia_directory_size($path) {
8
9
$size = 0;
10
11
$path .= DIRECTORY_SEPARATOR;
12
}
13
14
15
16
17
return false;
18
}
19
20
$queue = array($path);
21
22
$parent = $i;
23
24
$subdirs = array();
25
while (false !== ($entry = $dir->read())) {
26
if ($entry == '.' || $entry == '..') {
27
continue;
28
}
29
30
$path = $queue[$i] . $entry;
31
32
$path .= DIRECTORY_SEPARATOR;
33
$subdirs[] = $path;
34
35
36
}
37
}
38
39
unset($queue[0]);
40
41
$i = -1;
42
43
44
$dir->close();
45
unset($dir);
46
}
47
}
48
return $size;
49
}
Usage is as follows:
Download
Title: Return the Size of an Entire Directory with PHP
Description: Return the Size of an Entire Directory with PHP.
Download • Version 0.2, 709.0B, zip, Category: PHP Code & Snippets