DEV Community

Cover image for Calculating File Size with PHP for Local Data
andysaktia
andysaktia

Posted on • Updated on

Calculating File Size with PHP for Local Data

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

Prerequire

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.

$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
            ),
        );
Enter fullscreen mode Exit fullscreen mode

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.

 $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;
        }
    } 
Enter fullscreen mode Exit fullscreen mode

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:

  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;
  }
Enter fullscreen mode Exit fullscreen mode

Done

Top comments (0)