Home arrow PHP arrow Page 2 - PHP Tools for Working with the File and Operating System

Calculating File, Directory, and Disk Sizes - PHP

In this second part of an eight-part article series on the tools PHP provides for working with the file system and operating system, we'll go over file types, links, and how to calculate file, directory and disk sizes. This article is excerpted from chapter 10 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

  1. PHP Tools for Working with the File and Operating System
  2. Calculating File, Directory, and Disk Sizes
By: Apress Publishing
Rating: starstarstarstarstar / 0
November 09, 2011

print this article



Calculating file, directory, and disk sizes is a common task in all sorts of applications. This section introduces a number of standard PHP functions suited to this task.


int filesize (string filename)

The filesize() function returns the size, in bytes, of filename. An example follows:

$file = "/www/htdocs/book/chapter1.pdf";
$bytes = filesize("$file"); // Returns 91815
echo "File ".basename($file)." is $bytes bytes, or
".round($bytes / 1024, 2)." kilobytes.";

This returns the following:

--------------------------------------------File 852Chapter16R.rtf is 91815 bytes, or 89.66 kilobytes


float disk_free_space (string directory)

The disk_free_space() function returns the available space, in bytes, allocated to the disk partition housing the directory specified by directory. An example follows:

$drive = "/usr";
echo round((disk_free_space($drive) / 1048576), 2);

This returns:


Note that the returned number is in megabytes (MB), because the value returned from disk_free_space() was divided by 1,048,576, which is equivalent to 1MB.


float disk_total_space (string directory)

The disk_total_space() function returns the total size, in bytes, consumed by the disk partition housing the directory specified by directory. If you use this function in conjunction with disk_free_space(), it's easy to offer useful space allocation statistics:

$systempartitions = array("/", "/home","/usr", "/www");
foreach ($systempartitions as $partition) {
$totalSpace = disk_total_space($partition) / 1048576;
$usedSpace = $totalSpace - disk_free_space($partition) / 1048576;
echo "Partition: $partition (Allocated: $totalSpace MB.
Used: $usedSpace MB.)";

This returns:

--------------------------------------------Partition: / (Allocated: 3099.292 MB. Used: 343.652 MB.)
Partition: /home (Allocated: 5510.664 MB. Used: 344.448 MB.)
Partition: /usr (Allocated: 4127.108 MB. Used: 1985.716 MB.) Partition: /usr/local/apache2/htdocs (Allocated: 4127.108 MB. Used: 1985.716 MB.)

Please check back for the next part of the series.

>>> More PHP Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: