Calculating File Size with PHP for Local Data
Tags: tutorial, programming, php, webdev
Published: 2020-09-08 06:12:34

The following function only works when the files are in the same server environment.

### Prerequire
- Understand php built-in functions; [floatval](https://www.php.net/manual/en/function.floatval.php), [pow](https://www.php.net/manual/en/function.pow.php), [str_replace](https://www.php.net/manual/en/function.str-replace.php), [strval](https://www.php.net/manual/en/function.strval.php ), and [round](https://www.php.net/manual/en/function.round.php).
- Creating [multidimensional array](https://www.w3schools.com/PHP/php_arrays_multidimensional.asp) and how to call items in array using [foreach](https://www.php.net/manual/en/ control-structures.foreach.php).
- How to create a [function](https://www.w3schools.com/php/php_functions.asp).

### Prepare Array
The array that needs to be prepared is an array that is used to filter the size of the data to be checked with units of bytes.

```php
$arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );
```
It can be seen that in a multidimensional array there are several unit bytes whose value or `VALUE` is a value representing `UNIT`. It can be directly in the form of numbers or use `pow` or exponents, for example `pow(1024,2)` means 1024 to the power of 2 which is around 1 million. Using this `pow` function helps shorten writing.

### Process data
Data processing can be carried out immediately after first retrieving byte data with the `filesize()` function. Once done, the filter can be processed directly by utilizing the filter array that we have created.

```php
 $bytes = filesize('url_file');
 $bytes = floatval($bytes);
 
 foreach($arBytes as $arItem){
        if($bytes >= $arItem["VALUE"])
        {
            $result = $bytes / $arItem["VALUE"];
            $result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];
            break;
        }
    } 
```
Use `floatval` to ensure the data is well selected and is numeric. `foreach` will call the filter array data and determine the outgoing data by the `if` statement in it. The `$bytes` data is compared to each `VALUE` data in the filter array and if the condition `>=` is met the new `$bytes` will be handled. The process of finding the final result or `$result` is carried out in two stages, the first stage is by dividing the data by the array value according to the above conditions. The second stage, installation of `UNIT` based on the processed value, and changing the dot attribute to a comma after the rounding process by the `round` function.

Overall the script can be as follows:

```php
  function FileSizeConvert($url){
    $bytes = filesize($url);
    $bytes = floatval($bytes);
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

    foreach($arBytes as $arItem){
        if($bytes >= $arItem["VALUE"])
        {
            $result = $bytes / $arItem["VALUE"];
            $result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];
            break;
        }
    }
    return $result;
  }
```
***Done***