Current Location: Home> Latest Articles> How to Quickly Check Disk Free Space with PHP’s disk_free_space Function?

How to Quickly Check Disk Free Space with PHP’s disk_free_space Function?

gitbox 2025-09-30

In daily web development, managing server storage space is an essential task. PHP provides a very convenient function called disk_free_space(), which allows us to quickly check the available disk space on the server. This article will explain how to use this function to retrieve disk free space and how to apply it in practical project scenarios.

What is the disk_free_space() Function?

disk_free_space() is a built-in PHP function that returns the available space (in bytes) on the disk partition of a specified directory. If you want to know how much free space is left on a disk partition, this function makes it easy to retrieve that information.

The basic syntax of the function is as follows:

<span><span><span class="hljs-title function_ invoke__">disk_free_space</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$directory</span></span><span>): </span><span><span class="hljs-keyword">int</span></span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>

Parameter:

  • $directory: The path to the specified directory. This can be any path on a disk partition, typically passed as a directory path such as / or C:/.

Return Value:

  • The return value is an integer representing the available disk space in bytes. If an error occurs, the function returns false.

Example: How to Check Disk Free Space on a Server

Suppose we want to check the available disk space of the root directory /. We can use the following code:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$directory</span></span><span> = </span><span><span class="hljs-string">&#039;/&#039;</span></span><span>;
</span><span><span class="hljs-variable">$freeSpace</span></span><span> = </span><span><span class="hljs-title function_ invoke__">disk_free_space</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>);
<p></span>if ($freeSpace === false) {<br>
echo "Unable to retrieve disk space information";<br>
} else {<br>
echo "Available space: " . $freeSpace . " bytes";<br>
}<br>
?><br>
</span>

When running the above code, if the root directory’s disk has available space, $freeSpace will return the remaining space in bytes. If an error occurs, the function will return false, which allows us to check whether the information was successfully retrieved.

How to Convert Bytes into More Readable Units?

Although disk_free_space() returns the result in bytes, in practice we often need to convert it into more understandable units such as MB or GB. To do this, we can write a simple function:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">formatSize</span></span><span>(</span><span><span class="hljs-params"><span class="hljs-variable">$size</span></span></span><span>) {
    </span><span><span class="hljs-variable">$units</span></span><span> = [</span><span><span class="hljs-string">&#039;B&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;KB&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;MB&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;GB&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;TB&#039;</span></span><span>];
    </span><span><span class="hljs-variable">$unitIndex</span></span><span> = </span><span><span class="hljs-number">0</span></span>;
    </span><span><span class="hljs-variable">$size</span></span> /= </span><span><span class="hljs-number">1024</span></span>;
    </span><span><span class="hljs-variable">$unitIndex</span></span>++;
}

</span><span><span class="hljs-keyword">return</span></span> </span><span><span class="hljs-title function_ invoke__">round</span></span>(</span><span><span class="hljs-variable">$size</span></span>, </span><span><span class="hljs-number">2</span></span>) . </span><span><span class="hljs-string">&#039; &#039;</span></span> . </span><span><span class="hljs-variable">$units</span></span>[</span><span><span class="hljs-variable">$unitIndex</span></span>];

}

$directory = '/';
$freeSpace = disk_free_space($directory);

if ($freeSpace === false) {
echo "Unable to retrieve disk space information";
} else {
echo "Available space: " . formatSize($freeSpace);
}
?>

With the formatSize() function, we can easily convert bytes into more understandable units such as KB, MB, or GB.

How to Monitor Disk Free Space?

In some scenarios, developers may need to monitor disk free space regularly to avoid service interruptions caused by insufficient storage. You can set up scheduled tasks to run the above PHP script periodically or integrate disk monitoring logic into your application.

For example, you could add a scheduled task in your web application’s backend to check disk space regularly. If the available space drops below a certain threshold, it can send an alert notification to administrators.

Conclusion

By using PHP’s disk_free_space() function, we can easily retrieve available disk space, helping us identify storage issues early and prevent crashes or service downtime. In practical applications, combining this with scheduled tasks and threshold settings can provide a more robust storage monitoring mechanism for web applications.