Display Disk Free Space Percentage In PHP

Overview – Display Disk Free Space Percentage In PHP

Have you ever needed a nice little progress bar on your website to display disk space free percentage? This can be handy if you would like to give your clients an indication on how much disk space free they have on their own virtually hosted website, or perhaps you need a simple web interface to keep an eye out on your server health. In any case, this progress bar will accurately show you how much disk space free and disk space usage on your server.

Checkout out a follow up article called showing an alternate way to determine the total size of a folder using php.

End Result – What This Article Will Achieve

Get Total Directory Size In PHP

Get Total Directory Size In PHP

Once you have copied and implemented the code shows in the 3 steps, you should have a tidy progress bar that will show you your disk usage on your server. Below is an image of what it should look like if you have followed the steps correctly.

 

Step 1 – PHP Code

Place this code into a PHP script inside the web directory of your choice. All three steps can be included into the same PHP file or seperated if requried.

<?php

/* get disk space free (in bytes) */
$df = disk_free_space("/var/www");
/* and get disk space total (in bytes)  */
$dt = disk_total_space("/var/www");
/* now we calculate the disk space used (in bytes) */
$du = $dt - $df;
/* percentage of disk used - this will be used to also set the width % of the progress bar */
$dp = sprintf('%.2f',($du / $dt) * 100);

/* and we formate the size from bytes to MB, GB, etc. */
$df = formatSize($df);
$du = formatSize($du);
$dt = formatSize($dt);

function formatSize( $bytes )
{
	$types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
	for( $i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
		return( round( $bytes, 2 ) . " " . $types[$i] );
}

?>

Step 2 – CSS Style Sheet

Place this code into a CSS style sheet inside the web directory of your choice. The CSS code can be included into the same PHP file created in step 1 or seperated into its own CSS file if required, however since it is executing php code to create the width of the element, it should not be placeed inside a separate css file.

<style type='text/css'>

.progress {
	border: 2px solid #5E96E4;
	height: 32px;
	width: 540px;
	margin: 30px auto;
}
.progress .prgbar {
	background: #A7C6FF;
	width: <?php echo $dp; ?>%;
	position: relative;
	height: 32px;
	z-index: 999;
}
.progress .prgtext {
	color: #286692;
	text-align: center;
	font-size: 13px;
	padding: 9px 0 0;
	width: 540px;
	position: absolute;
	z-index: 1000;
}
.progress .prginfo {
	margin: 3px 0;
}

</style>

Step 3 – HTML Code Including the Progress Bar

Place this code into an HTML file inside the web directory of your choice. The HTML code can be included into the same PHP file created in step 1 or seperated into its own file if required.

<div class='progress'>
	<div class='prgtext'><?php echo $dp; ?>% Disk Used</div>
	<div class='prgbar'></div>
	<div class='prginfo'>
		<span style='float: left;'><?php echo "$du of $dt used"; ?></span>
		<span style='float: right;'><?php echo "$df of $dt free"; ?></span>
		<span style='clear: both;'></span>
	</div>
</div>