Prev | Current Page 344 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


This section introduces a number of standard PHP functions suited to this task.
Determining a File??™s Size
The filesize() function returns the size, in bytes, of a specified file. Its prototype
follows:
int filesize(string filename)
An example follows:
$file = "/www/htdocs/book/chapter1.pdf";
$bytes = filesize($file);
$kilobytes = round($bytes/1024, 2);
printf("File %s is $bytes bytes, or %.2f kilobytes",
basename($file), $kilobytes);
?>
This returns the following:
File chapter1.pdf is 91815 bytes, or 89.66 kilobytes
Calculating a Disk??™s Free Space
The function disk_free_space() returns the available space, in bytes, allocated to the
disk partition housing a specified directory. Its prototype follows:
float disk_free_space(string directory)
282 CHAPTER 10 ?–  WORKING WITH THE FI LE AND OPERATING SYSTEM
An example follows:
$drive = "/usr";
printf("Remaining MB on %s: %.2f", $drive,
round((disk_free_space($drive) / 1048576), 2));
?>
This returns the following:
Remaining MB on /usr: 2141.29
Note that the returned number is in megabytes (MB) because the value returned
from disk_free_space() is divided by 1,048,576, which is equivalent to 1MB.
Calculating Total Disk Size
The disk_total_space() function returns the total size, in bytes, consumed by the
disk partition housing a specified directory. Its prototype follows:
float disk_total_space(string directory)
If you use this function in conjunction with disk_free_space(), it??™s easy to offer
useful space allocation statistics:
$partition = "/usr";
// Determine total partition space
$totalSpace = disk_total_space($partition) / 1048576;
// Determine used partition space
$usedSpace = $totalSpace - disk_free_space($partition) / 1048576;
printf("Partition: %s (Allocated: %.


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