daily pastebin goal
71%
SHARE
TWEET

Untitled

a guest Feb 16th, 2014 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Disk Status Class
  5.  *
  6.  * http://pmav.eu/stuff/php-disk-status/
  7.  *
  8.  * v1.0 - 17/Oct/2008
  9.  * v1.1 - 22/Ago/2009 (Exceptions added.)
  10.  */
  11.  
  12. class DiskStatus {
  13.  
  14.   const RAW_OUTPUT = true;
  15.  
  16.   private $diskPath;
  17.  
  18.  
  19.   function __construct($diskPath) {
  20.     $this->diskPath = $diskPath;
  21.   }
  22.  
  23.  
  24.   public function totalSpace($rawOutput = false) {
  25.     $diskTotalSpace = @disk_total_space($this->diskPath);
  26.  
  27.     if ($diskTotalSpace === FALSE) {
  28.       throw new Exception('totalSpace(): Invalid disk path.');
  29.     }
  30.  
  31.     return $rawOutput ? $diskTotalSpace : $this->addUnits($diskTotalSpace);
  32.   }
  33.  
  34.  
  35.   public function freeSpace($rawOutput = false) {
  36.     $diskFreeSpace = @disk_free_space($this->diskPath);
  37.  
  38.     if ($diskFreeSpace === FALSE) {
  39.       throw new Exception('freeSpace(): Invalid disk path.');
  40.     }
  41.  
  42.     return $rawOutput ? $diskFreeSpace : $this->addUnits($diskFreeSpace);
  43.   }
  44.  
  45.  
  46.   public function usedSpace($precision = 1) {
  47.     try {
  48.       return round((100 - ($this->freeSpace(self::RAW_OUTPUT) / $this->totalSpace(self::RAW_OUTPUT)) * 100), $precision);
  49.     } catch (Exception $e) {
  50.       throw $e;
  51.     }
  52.   }
  53.  
  54.  
  55.   public function getDiskPath() {
  56.     return $this->diskPath;
  57.   }
  58.  
  59.  
  60.   private function addUnits($bytes) {
  61.     $units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
  62.  
  63.     for($i = 0; $bytes >= 1024 && $i < count($units) - 1; $i++ ) {
  64.       $bytes /= 1024;
  65.     }
  66.  
  67.     return round($bytes, 1).' '.$units[$i];
  68.   }
  69.  
  70. }
  71.  
  72. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top