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 * @param $str
* @return bool|int * @return bool|int
*/ */
static function parse_filesize($str) { /* {{{ */ static function parse_filesize($str) { /* {{{ */
preg_replace('/\s\s+/', '', $str); if(!preg_match('/^([0-9]+) *([BKMGT]*)$/', trim($str), $matches))
if(in_array(strtoupper(substr($str, -1)), array('B','K','M','G'))) { return false;
$value = (int) substr($str, 0, -1); $value = $matches[1];
$unit = substr($str, -1, 1); $unit = $matches[2] ? $matches[2] : 'B';
} else { switch($unit) {
return (int) $str; case 'T':
} return $value * 1024 * 1024 * 1024 *1024;
switch(strtoupper($unit)) { break;
case 'G': case 'G':
return $value * 1024 * 1024 * 1024; return $value * 1024 * 1024 * 1024;
break; break;
@ -143,13 +147,21 @@ class SeedDMS_Core_File {
return $value * 1024; return $value * 1024;
break; break;
default; default;
return $value; return (int) $value;
break; break;
} }
/** @noinspection PhpUnreachableStatementInspection */ /** @noinspection PhpUnreachableStatementInspection */
return false; return false;
} /* }}} */ } /* }}} */
/**
* @param $file
* @return string
*/
static function file_exists($file) { /* {{{ */
return file_exists($file);
} /* }}} */
/** /**
* @param $file * @param $file
* @return string * @return string