HomePHP Page 11 - File And Directory Manipulation In PHP (part 2)
Size Does Matter - PHP
Now that you know the basics of reading and writing files, this second segment of our tutorial on the PHP filesystem API takes you into deeper waters, showing you how to copy, delete and rename files; scan directories; work with uploaded files over HTTP; perform pattern matches on file names; and read and write to processes instead of files.
If you're looking for information on the total size of a partition or mount point, PHP offers the relatively-new disk_total_space() and disk_free_space() functions, which return the total available space and total free space, in bytes, on a particular partition. Consider the following example, which demonstrates:
<?php
// set partition
$fs = "/";
// display available and used space
echo "Total available space: " . round(disk_total_space($fs) / (1024*1024))
. " MB\r\n"; echo "Total free space: " . round(disk_free_space($fs)
/ (1024*1024)) . " MB\r\n";
// calculate used space
$disk_used_space =
round((disk_total_space($fs) - disk_free_space($fs)) / (1024*1024));
echo "Total used space: " . $disk_used_space . " MB\r\n";
// calculate % used space
echo "% used space: " . round((disk_total_space($fs) -
disk_free_space($fs)) / disk_total_space($fs) * 100) . " %";
?>
Here's an example of what the output might look like:
Total available space: 7906 MB Total free space: 4344 MB Total used space: 3562 MB % used space: 45 %