Prev | Current Page 345 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

2f MB. Used: %.2f MB.)",
$partition, $totalSpace, $usedSpace);
?>
CHAPTER 10 ?–  WORKING WITH T HE FILE A ND OPERATING SYSTEM 283
This returns the following:
Partition: /usr (Allocated: 36716.00 MB. Used: 32327.61 MB.)
Retrieving a Directory Size
PHP doesn??™t currently offer a standard function for retrieving the total size of a directory,
a task more often required than retrieving total disk space (see disk_total_space()
in the previous section). And although you could make a system-level call to du using
exec() or system() (both of which are introduced in the later section ???PHP??™s Program
Execution Functions???), such functions are often disabled for security reasons. The
alternative solution is to write a custom PHP function that is capable of carrying out
this task. A recursive function seems particularly well-suited for this task. One possible
variation is offered in Listing 10-1.
?– Note The du command will summarize disk usage of a file or a directory. See the appropriate man
page for usage information.
Listing 10-1. Determining the Size of a Directory??™s Contents
function directory_size($directory) {
$directorySize=0;
// Open the directory and read its contents.
if ($dh = @opendir($directory)) {
// Iterate through each directory entry.
while (($filename = readdir ($dh))) {
// Filter out some of the unwanted directory entries.
if ($filename != ".


Pages:
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357