Skip to content

Instantly share code, notes, and snippets.

@dastanaron
Created January 23, 2018 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dastanaron/d39870793eb6acad24c527d78994db45 to your computer and use it in GitHub Desktop.
Save dastanaron/d39870793eb6acad24c527d78994db45 to your computer and use it in GitHub Desktop.
Class FreeSpace for calculating hard disk space
<?php
namespace backend\components;
/**
* Class FreeSpace for calculating free hard disk space
* only php 7.0 or later
* @package backend\components
*/
class DiskSpace
{
/**
* @param string $dir default(__DIR__)
* @param string $unit
* @values $unit (kb, mb, 'gb' or empty)
* @return float
*/
public static function showFree(string $dir = __DIR__, string $unit = null) : float
{
$freeSpaceByte = self::getFreeSpace($dir);
return self::calcSize($freeSpaceByte, $unit);
}
/**
* @param string $dir default(__DIR__)
* @param string $unit
* @values $unit (kb, mb, 'gb' or empty)
* @return float
*/
public static function showTotal(string $dir = __DIR__, string $unit = null) : float
{
$totalSpaceByte = self::getTotalSpace($dir);
return self::calcSize($totalSpaceByte, $unit);
}
/**
* Returns the difference between the total size of the disk space and the free space, in other words, returns the occupied space
* @param string $dir
* @param string $unit
* @values $unit (kb, mb, 'gb' or empty)
* @return float
*/
public static function getOccupied(string $dir = __DIR__, string $unit = null) : float
{
$freeSpaceByte = self::getFreeSpace($dir);
$totalSpaceByte = self::getTotalSpace($dir);
$different = $totalSpaceByte - $freeSpaceByte;
return self::calcSize($different, $unit);
}
/**
* Method returns an array of values of different units of measure
* @param string $dir default(__DIR__)
* @return array
*/
public static function getArrayFreeSpace(string $dir = __DIR__) : array
{
$freeSpaceByte = self::getFreeSpace($dir);
return [
'byte' => $freeSpaceByte,
'kb' => self::calcSize($freeSpaceByte, 'kb'),
'mb' => self::calcSize($freeSpaceByte, 'mb'),
'gb' => self::calcSize($freeSpaceByte, 'gb'),
];
}
/**
* Method returns an array of values of different units of measure
* @param string $dir default(__DIR__)
* @return array
*/
public static function getArrayTotalSpace(string $dir = __DIR__) : array
{
$totalSpaceByte = self::getTotalSpace($dir);
return [
'byte' => $totalSpaceByte,
'kb' => self::calcSize($totalSpaceByte, 'kb'),
'mb' => self::calcSize($totalSpaceByte, 'mb'),
'gb' => self::calcSize($totalSpaceByte, 'gb'),
];
}
/**
* If you need to change the system of obtaining free space,
* you need to override this method, now it uses `disk_free_space`
* @param string $dir default(__DIR__)
* @return bool|float
*/
protected static function getFreeSpace(string $dir = __DIR__) : float
{
return disk_free_space ($dir);
}
/**
* If you need to change the system of obtaining total space,
* you need to override this method, now it uses `disk_free_space`
* @param string $dir default(__DIR__)
* @return bool|float
*/
protected static function getTotalSpace(string $dir = __DIR__) : float
{
return disk_total_space($dir);
}
/**
* @param $byte_size
* @param $unit
* @values $unit (kb, mb, 'gb')
* @return float
*/
protected static function calcSize(float $byte_size, string $unit) : float
{
switch ($unit) {
case 'kb':
return round($byte_size/1024, 2);
case 'mb':
return round($byte_size/1024/1024, 2);
case 'gb':
return round($byte_size/1024/1024/1024);
default:
return $byte_size;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment