Skip to content

Instantly share code, notes, and snippets.

@imanilchaudhari
Created July 14, 2016 10:04
Show Gist options
  • Save imanilchaudhari/b383e94175d527b3ada8755cae4c3582 to your computer and use it in GitHub Desktop.
Save imanilchaudhari/b383e94175d527b3ada8755cae4c3582 to your computer and use it in GitHub Desktop.
<?php
namespace backend\components;
class DiskStatus {
const RAW_OUTPUT = true;
private $diskPath;
function __construct($diskPath) {
$this->diskPath = $diskPath;
}
public function totalSpace($rawOutput = false) {
$diskTotalSpace = @disk_total_space($this->diskPath);
if ($diskTotalSpace === FALSE) {
throw new Exception('totalSpace(): Invalid disk path.');
}
return $rawOutput ? $diskTotalSpace : $this->addUnits($diskTotalSpace);
}
public function freeSpace($rawOutput = false) {
$diskFreeSpace = @disk_free_space($this->diskPath);
if ($diskFreeSpace === FALSE) {
throw new Exception('freeSpace(): Invalid disk path.');
}
return $rawOutput ? $diskFreeSpace : $this->addUnits($diskFreeSpace);
}
public function usedSpace($precision = 1) {
try {
return round((100 - ($this->freeSpace(self::RAW_OUTPUT) / $this->totalSpace(self::RAW_OUTPUT)) * 100), $precision);
} catch (Exception $e) {
throw $e;
}
}
public function getDiskPath() {
return $this->diskPath;
}
private function addUnits($bytes) {
$units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
for($i = 0; $bytes >= 1024 && $i < count($units) - 1; $i++ ) {
$bytes /= 1024;
}
return round($bytes, 1).' '.$units[$i];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment