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 are the best practices for structuring PHP code to avoid syntax errors and unexpected behavior?
- How can you ensure that your PHP code is secure when dealing with file permissions?
- In PHP, what are some considerations to keep in mind when implementing image file upload functionality to ensure security and prevent malicious uploads?