diff --git a/SeedDMS_Core/Core/inc.FileUtils.php b/SeedDMS_Core/Core/inc.FileUtils.php index ac09421b0..b9e6e638b 100644 --- a/SeedDMS_Core/Core/inc.FileUtils.php +++ b/SeedDMS_Core/Core/inc.FileUtils.php @@ -121,18 +121,22 @@ class SeedDMS_Core_File { } /* }}} */ /** + * Parses a string like '[0-9]+ *[BKMGT]*' into an integer + * B,K,M,G,T stand for byte, kilo byte, mega byte, giga byte, tera byte + * If the last character is omitted, bytes are assumed. + * * @param $str * @return bool|int */ static function parse_filesize($str) { /* {{{ */ - preg_replace('/\s\s+/', '', $str); - if(in_array(strtoupper(substr($str, -1)), array('B','K','M','G'))) { - $value = (int) substr($str, 0, -1); - $unit = substr($str, -1, 1); - } else { - return (int) $str; - } - switch(strtoupper($unit)) { + if(!preg_match('/^([0-9]+) *([BKMGT]*)$/', trim($str), $matches)) + return false; + $value = $matches[1]; + $unit = $matches[2] ? $matches[2] : 'B'; + switch($unit) { + case 'T': + return $value * 1024 * 1024 * 1024 *1024; + break; case 'G': return $value * 1024 * 1024 * 1024; break; @@ -143,13 +147,21 @@ class SeedDMS_Core_File { return $value * 1024; break; default; - return $value; + return (int) $value; break; } /** @noinspection PhpUnreachableStatementInspection */ return false; } /* }}} */ + /** + * @param $file + * @return string + */ + static function file_exists($file) { /* {{{ */ + return file_exists($file); + } /* }}} */ + /** * @param $file * @return string