Posted By

mscribellito on 01/08/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

AllKnightAccess


PHP Disk Space Class


 / Published in: PHP
 

  1. class Disk {
  2.  
  3. public static final function totalSpace($format = FALSE) {
  4.  
  5. $totalSpace = disk_total_space('/');
  6.  
  7. return ($format === TRUE) ? self::formatSize($totalSpace) : $totalSpace;
  8.  
  9. }
  10.  
  11. public static final function freeSpace($format = FALSE) {
  12.  
  13. $freeSpace = disk_free_space('/');
  14.  
  15. return ($format === TRUE) ? self::formatSize($freeSpace) : $freeSpace;
  16.  
  17. }
  18.  
  19. public static final function usedSpace($format = FALSE) {
  20.  
  21. $usedSpace = self::totalSpace() - self::freeSpace();
  22.  
  23. return ($format === TRUE) ? self::formatSize($usedSpace) : $usedSpace;
  24.  
  25. }
  26.  
  27. public static final function formatSize($size = 0){
  28.  
  29. switch (true){
  30.  
  31. case ($size > 1099511627776):
  32. $size /= 1099511627776;
  33. $suffix = ' TB';
  34. break;
  35.  
  36. case ($size > 1073741824):
  37. $size /= 1073741824;
  38. $suffix = ' GB';
  39. break;
  40.  
  41. case ($size > 1048576):
  42. $size /= 1048576;
  43. $suffix = ' MB';
  44. break;
  45.  
  46. case ($size > 1024):
  47. $size /= 1024;
  48. $suffix = ' KB';
  49. break;
  50.  
  51. default:
  52. $suffix = ' B';
  53.  
  54. }
  55.  
  56. return round($size, 2) . $suffix;
  57.  
  58. }
  59.  
  60. }

Report this snippet  

You need to login to post a comment.