PHP Filesystem disk_free_space() Function
The PHP Filesystem disk_free_space() function is used to determine how much free space is on a disc or disc partition. It offers programmers with a simple way to track the amount of disc space that PHP programs are using.
Given a string containing a directory, the disk_free_space() function can return the number of bytes available on corresponding filesystem or disk partition.
Syntax
Following is the syntax of the PHP Filesystem disk_free_space() function −
float disk_free_space ( string $directory )
Parameters
Here are the required parameter of the disk_free_space() function −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
directory(Required) This path points to the directory or disc partition that you want to look at. |
Return Value
The function returns the number of bytes that are available in the given directory or disc partition. If the directory is not valid, it returns FALSE.
PHP Version
The disk_free_space() function is available as part of core PHP 4.1.0 and work well with the PHP 5, PHP 7, PHP 8.
Example
Since the PHP filesystem disk_free_space() function returns number of bytes or FALSE so you can use conditional expressions, like if statements and ternary operators, to see this. Check the example of code below −
<?php
$directory = "/"; // Root directory
$freeSpace = disk_free_space($directory);
if ($freeSpace !== FALSE) {
echo "Free space in bytes: " . $freeSpace;
} else {
echo "Could not determine free space.";
}
?>
Output
This will produce the following result −
Free space in bytes: 129435885568
Example
To show the basic usage of the disk_free_space() function we have just passed the disk path or disk name. And it will return the available disk space information in bytes.
<?php
echo disk_free_space("C:");
echo "\n";
echo disk_free_space("E:");
?gt;
Output
This will generate the below result −
224150941696 209571344384
Example
Now we will put an specific directory path to get the available space in the particular path with the help of disk_free_space().
<?php
// Specific directory
$directory = "/Applications/XAMPP/xamppfiles/htdocs/mac";
$freeSpace = disk_free_space($directory);
if ($freeSpace !== FALSE) {
echo "Free space on $directory: " . $freeSpace;
} else {
echo "Could not determine free space on $directory.";
}
?>
Output
This will produce the below output −
Free space on /Applications/XAMPP/xamppfiles/htdocs/mac: 116797157376
Example
Using the below code we will check the disk space available in the root directory and see how we can use disk_free_space() function in this case. And after getting the free space we will try to format the value which is present in bytes so we will convert it in the KB, MB or GB for easy readability using formatBytes() function.
<?php
// Root directory
$directory = "/";
$freeSpace = disk_free_space($directory);
if ($freeSpace !== FALSE) {
echo "Free space on our root directory: " . formatBytes($freeSpace);
} else {
echo "Could not determine the free space.";
}
function formatBytes($bytes, $precision = 2) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$pow = floor(log(max($bytes, 1)) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
?>
Output
This will create the below outcome −
Free space on root directory: 108.78 GB
Example
Now, we are going to try to resolve the error so that our application functions better. Dealing with errors that result from specific unwanted values is usually preferable.
<?php
// Invalid directory
$directory = "Applications/XAMPP/xamppfiles/htdocs/invalidDirectory";
// Use disk_free_space to find available free space
$freeSpace = disk_free_space($directory);
// Handle error here
if ($freeSpace !== FALSE) {
echo "Free space on $directory: " . $freeSpace;
} else {
echo "Could not determine free space on the directory. I think directory is not available";
}
?>
Output
This will produce the following outcome −
Could not determine free space on the directory. I think directory is not available
Note
- Verify that the directory path you entered is both valid and accessible. Incorrect paths will result in a FALSE return value.
- The function returns the amount of free space in bytes. For easy viewing, you can convert this to more readable quantities like KB, MB, GB, etc.
- It can be used in a range of scenarios because it is compatible with Windows, Linux, and macOS.
Summary
Programers can use PHP's disk_free_space() function to find out how much free space is available on a disc or directory inside their programs. It is easy to use and offers important information for managing and monitoring disc space.