DiskSpace

This commit is contained in:
Karl Hudgell 2015-07-02 19:51:14 +01:00
parent ac60d7871a
commit a5ac997098
2 changed files with 212 additions and 0 deletions

73
DiskStatus.class.php Normal file
View File

@ -0,0 +1,73 @@
<?php
/**
* Disk Status Class
*
* http://pmav.eu/stuff/php-disk-status/
*
* v1.0 - 17/Oct/2008
* v1.1 - 22/Ago/2009 (Exceptions added.)
*/
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];
}
}
?>

139
space.php Normal file
View File

@ -0,0 +1,139 @@
<html>
<head>
</head>
<body>
<?php
/**
* Disk Status Class - Example
*
* http://pmav.eu/stuff/php-disk-status/
*
* 22/Aug/2009
*/
require_once 'DiskStatus.class.php';
try {
$diskStatusC = new DiskStatus('c:');
$freeSpaceC = $diskStatusC->freeSpace();
$totalSpaceC = $diskStatusC->totalSpace();
$barWidthC = ($diskStatusC->usedSpace()/100) * 150;
} catch (Exception $e) {
echo 'Error ('.$e->getMessage().')';
exit();
}
try {
$diskStatus1 = new DiskStatus('d:');
$freeSpace1 = $diskStatus1->freeSpace();
$totalSpace1 = $diskStatus1->totalSpace();
$barWidth1 = ($diskStatus1->usedSpace()/100) * 150;
} catch (Exception $e) {
echo 'Error ('.$e->getMessage().')';
exit();
}
try {
$diskStatus2 = new DiskStatus('e:');
$freeSpace2 = $diskStatus2->freeSpace();
$totalSpace2 = $diskStatus2->totalSpace();
$barWidth2 = ($diskStatus2->usedSpace()/100) * 150;
} catch (Exception $e) {
echo 'Error ('.$e->getMessage().')';
exit();
}
try {
$diskStatus3 = new DiskStatus('f:');
$freeSpace3 = $diskStatus3->freeSpace();
$totalSpace3 = $diskStatus3->totalSpace();
$barWidth3 = ($diskStatus3->usedSpace()/100) * 150;
} catch (Exception $e) {
echo 'Error ('.$e->getMessage().')';
exit();
}
try {
$diskStatus4 = new DiskStatus('g:');
$freeSpace4 = $diskStatus4->freeSpace();
$totalSpace4 = $diskStatus4->totalSpace();
$barWidth4 = ($diskStatus4->usedSpace()/100) * 150;
} catch (Exception $e) {
echo 'Error ('.$e->getMessage().')';
exit();
}
try {
$diskStatus5 = new DiskStatus('h:');
$freeSpace5 = $diskStatus5->freeSpace();
$totalSpace5 = $diskStatus5->totalSpace();
$barWidth5 = ($diskStatus5->usedSpace()/100) * 150;
} catch (Exception $e) {
echo 'Error ('.$e->getMessage().')';
exit();
}
try {
$diskStatus6 = new DiskStatus('j:');
$freeSpace6 = $diskStatus6->freeSpace();
$totalSpace6 = $diskStatus6->totalSpace();
$barWidth6 = ($diskStatus6->usedSpace()/100) * 150;
} catch (Exception $e) {
echo 'Error ('.$e->getMessage().')';
exit();
}
?>
</p>
<table border=0>
<tr><td BGCOLOR='#0066FF'><font color='white'>Disk Space</font></td></tr>
<tr><td>C:\<?= $freeSpaceC ?> (of <?= $totalSpaceC ?>)</td></tr>
<tr><td><div class="disk">
<div class="used" style="width: <?= $barWidthC ?>px"><?= $diskStatusC->usedSpace() ?>%&nbsp;</div>
</div></td></tr>
<tr><td>D:\<?= $freeSpace1 ?> (of <?= $totalSpace1 ?>)</td></tr>
<tr> <tr><td><div class="disk">
<div class="used" style="width: <?= $barWidth1 ?>px"><?= $diskStatus1->usedSpace() ?>%&nbsp;</div>
</div></td></tr>
<tr><td>E:\<?= $freeSpace2 ?> (of <?= $totalSpace2 ?>)</td></tr>
<tr> <tr><td><div class="disk">
<div class="used" style="width: <?= $barWidth2 ?>px"><?= $diskStatus2->usedSpace() ?>%&nbsp;</div>
</div></td></tr>
<tr><td>F:\<?= $freeSpace3 ?> (of <?= $totalSpace3 ?>)</td></tr>
<tr> <tr><td><div class="disk">
<div class="used" style="width: <?= $barWidth3 ?>px"><?= $diskStatus3->usedSpace() ?>%&nbsp;</div>
</div></td></tr>
<tr><td>G:\<?= $freeSpace4 ?> (of <?= $totalSpace4 ?>)</td></tr>
<tr> <tr><td><div class="disk">
<div class="used" style="width: <?= $barWidth4 ?>px"><?= $diskStatus4->usedSpace() ?>%&nbsp;</div>
</div></td></tr>
<tr><td>H:\<?= $freeSpace5 ?> (of <?= $totalSpace5 ?>)</td></tr>
<tr> <tr><td><div class="disk">
<div class="used" style="width: <?= $barWidth5 ?>px"><?= $diskStatus5->usedSpace() ?>%&nbsp;</div>
</div></td></tr>
<tr><td>J:\<?= $freeSpace6 ?> (of <?= $totalSpace6 ?>)</td></tr>
<tr> <tr><td><div class="disk">
<div class="used" style="width: <?= $barWidth6 ?>px"><?= $diskStatus6->usedSpace() ?>%&nbsp;</div>
</div></td></tr>
</table>
</body>
</html>