-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
209 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,14 @@ class FileHelper | |
* Gets name of the filename only (without dot and extension). | ||
* If the file name contains several dots (file.name.ext) - return all parts before the last dot. | ||
* | ||
* @param string $sFilename | ||
* @param string|null $sFilename | ||
* @param string $default | ||
* | ||
* @return string | ||
*/ | ||
public static function getFileNameOnly(?string $sFilename = '', string $default = ''): string | ||
{ | ||
if ('' === $sFilename || \is_null($sFilename)) { | ||
if (null === $sFilename || '' === $sFilename) { | ||
return $default; | ||
} | ||
|
||
|
@@ -41,19 +41,47 @@ public static function getFileNameOnly(?string $sFilename = '', string $default | |
/** | ||
* Gets filename extension only (without dot and name) | ||
* | ||
* @param string $sFilename | ||
* @param string|null $sFilename | ||
* @param string $default | ||
* | ||
* @return string | ||
*/ | ||
public static function getFileExtOnly(?string $sFilename = '', string $default = ''): string | ||
{ | ||
if ('' === $sFilename || \is_null($sFilename)) { | ||
if (null === $sFilename || '' === $sFilename) { | ||
return $default; | ||
} | ||
|
||
$sParsed = \pathinfo(\trim($sFilename), PATHINFO_EXTENSION); | ||
|
||
return $sParsed ? $sParsed : $default; | ||
} | ||
|
||
/** | ||
* Converts file size in bytes into human readable file size. | ||
* | ||
* Конвертация размера файла в удобный для чтения формат. | ||
* | ||
* @param mixed|null $bytes | ||
* @param int $decimals | ||
* | ||
* @return string human readable file size (2.87 MB) | ||
* | ||
* @author evgenij at kostanay dot kz | ||
* | ||
* @edit Leonid Sheikman <[email protected]> | ||
*/ | ||
public static function getHumanFileSize(?mixed $bytes, int $decimals = 2): string | ||
{ | ||
if (null === $bytes) { | ||
return '0'; | ||
} | ||
|
||
$factor = \floor((\strlen((string) $bytes) - 1) / 3); | ||
if ($factor > 0) { | ||
$sz = 'KMGT'; | ||
} | ||
|
||
return \sprintf("%.{$decimals}f", (float) $bytes / \pow(1024, $factor)) . ' ' . @$sz[$factor - 1] . 'B'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.