add file_exists(), improve parse_filesize()

This commit is contained in:
Uwe Steinmann 2022-11-17 11:32:05 +01:00
parent 7716d3ad54
commit 62c2606d09

View File

@ -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