PHP 8.5.0 Alpha 4 available for testing

Voting

: min(five, nine)?
(Example: nine)

The Note You're Voting On

Svetoslav Marinov
16 years ago
This is an updated version of my previous filesize2bytes.
The return type now it's really an int.

<?php
/**
* Converts human readable file size (e.g. 10 MB, 200.20 GB) into bytes.
*
* @param string $str
* @return int the result is in bytes
* @author Svetoslav Marinov
* @author http://slavi.biz
*/
function filesize2bytes($str) {
$bytes = 0;

$bytes_array = array(
'B' => 1,
'KB' => 1024,
'MB' => 1024 * 1024,
'GB' => 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024,
'PB' => 1024 * 1024 * 1024 * 1024 * 1024,
);

$bytes = floatval($str);

if (
preg_match('#([KMGTP]?B)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) {
$bytes *= $bytes_array[$matches[1]];
}

$bytes = intval(round($bytes, 2));

return
$bytes;
}
?>

<< Back to user notes page

To Top