Skip to content

Instantly share code, notes, and snippets.

@fredbradley
Last active March 10, 2022 09:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredbradley/6266dcbcbda46fc0e4bc to your computer and use it in GitHub Desktop.
Save fredbradley/6266dcbcbda46fc0e4bc to your computer and use it in GitHub Desktop.
Disk Space Checker

class.DiskSpaceCheck.php

This class is quick and simple to install into any PHP application, and is easy to customise to the display you require.

The script finds the amount of total disk space on your system, and then the total free space. We then calculate the used space and give it to you as a percentage.

Developer

This class was developed by Fred Bradley - http://twitter.com/fredbradley

Setting the path to check

You can choose the path of the disk that you want to check (if you have multiple disks mounted, for example). You can do this by setting the first paramater when calling calling the Class to the directory that you want to check. EG: $disk = new DiskSpaceCheck('/var/www/vhosts/');

To Install

  • Add the class.DiskSpaceCheck.php to you filesystem.
  • In your file that you wish to use this script create a new OO instsance of the class. $disk = new DiskSpaceCheck();
  • When you do this you will find 4 new variables available for you to pop into your frontend.
  • $disk->total_space
  • $disk->free_space
  • $disk->used_space
  • $disk->percent
  • There is also one function that included which is $disk->formatBytes($bytes, $precision=2) this will automatically translate the thousands of bytes digits into a simple to read MB, or GB - or even TB for you.

Example Frontend

Here is a quick example markup using Bootstrap.

<div class="push-bit">
	<span class="pull-right">
		<small><?php echo floor($disk->percent); ?>%</small>
	</span>
	<small><strong><?php echo $disk->formatBytes($disk->used_space); ?></strong> / <?php echo $disk->formatBytes($disk->total_space); ?></small>
</div>
<div class="progress progress-mini push-bit">
	<div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="<?php echo $disk->percent; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $disk->percent; ?>%"></div>
</div>

Version

1.0.0

<?php
class DiskSpaceCheck {
public $total_space = false;
public $free_space = false;
public $used_space = false;
public $percent = false;
function __construct($directory=null) {
if ($directory===null) {
$directory = dirname(__FILE__);
}
$this->total_space = disk_total_space($directory);
$this->free_space = disk_free_space($directory);
$this->used_space = $this->total_space-$this->free_space;
$this->percent = (($this->used_space/$this->total_space) * 100);
}
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 /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
}
<?php
$disk = new DiskSpaceCheck(dirname(__FILE__));
?>
<div class="push-bit">
<span class="pull-right">
<small><?php echo floor($disk->percent); ?>%</small>
</span>
<small><strong><?php echo $disk->formatBytes($disk->used_space); ?></strong> / <?php echo $disk->formatBytes($disk->total_space); ?></small>
</div>
<div class="progress progress-mini push-bit">
<div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="<?php echo $disk->percent; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $disk->percent; ?>%"></div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment