What are some PHP functions that can be used to read the total, free, and used disk space on a server?
To read the total, free, and used disk space on a server using PHP, you can use the disk_total_space() and disk_free_space() functions to get the total and free disk space respectively. You can then calculate the used disk space by subtracting the free space from the total space. This information can be useful for monitoring server storage capacity and managing disk usage.
$total_space = disk_total_space("/");
$free_space = disk_free_space("/");
$used_space = $total_space - $free_space;
echo "Total Disk Space: " . formatBytes($total_space) . "<br>";
echo "Free Disk Space: " . formatBytes($free_space) . "<br>";
echo "Used Disk Space: " . formatBytes($used_space) . "<br>";
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
Keywords
Related Questions
- What best practices should be followed when posting code-related questions on PHP forums to ensure effective and efficient troubleshooting by the community?
- How can PHP be used to read and write data to a text file without using MySQL?
- What potential issues can arise when using $_GET variables in PHP?