Calculate and convert size of a byte file to kB, MB, GB, etc.

0

Hello, I would like to know how I can supplement the code below so that filesize via remote URL returns the file size result in MB because the current code is returning the result of the file size in bytes .

function remotefileSize($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_exec($ch);
    $filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);
    if ($filesize) return $filesize;
}


echo remotefileSize('http://thumb.mais.uol.com.br/15233302-medium.jpg');
    
asked by anonymous 21.08.2015 / 08:08

4 answers

1

First of all, it is important to define that NOT is 1024, this has been resolved a long time ago.

To avoid confusion, since Kilo means 1000, they have defined the Kibi prefix that has the 1024 multiplier. So we have KiB, MiB, GiB and in some operating systems are already using the correct units. IEC Standard Prefixes

Now regarding the code, to be able to convert to arbitrary units:

function ConverterPrefixoBinario ($Valor, $UnidadeOrigem, $UnidadeDestino, $Precisão = 2)
{
    $Unidades = array (
        'B' => 1, 
        'KB' => 1000, 'MB' => 1000000, 'GB' => 1000000000, 'TB' => 1000000000,
        'KiB' => 1024, 'MiB' => 1048576, 'GiB' => 1073741824, 'TiB' => 1099511627776
    );

    $ValorBytes = $Valor * $Unidades [$UnidadeOrigem];

    return round($ValorBytes / $Unidades [$UnidadeDestino], $Precisão) . $UnidadeDestino;
}

// 0,15GiB
echo ConverterPrefixoBinario (150, 'MiB', 'GiB');

// 1,5MiB
echo ConverterPrefixoBinario (1572864, 'B', 'MiB');
    
21.08.2015 / 15:06
1

The basis of calculation is 1024 because 1kb is equivalent to 1024 bytes.

Based on this, create a function that determines the type of measure.

/*
    1024000 bytes = 1 kilobytes
    1000 kb = 1 megabyte
    1000 mb = 1 gigabyte
    e assim vai..
*/
    function filesize_formatted($path)
    {
        $size = filesize($path);
        $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $power = $size > 0 ? floor(log($size, 1024)) : 0;
        return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
    }

    echo filesize_formatted( 2048 );

In this example, in order to reduce the code, we use a PHP math function called pow() , which makes potential expression calculations.

Important, in this example returns the result formatted to 2 decimal places. The formatting of the result may vary with the need for each. So be aware and modify as needed.

Being more specific about your code, remove all unnecessary snippets:

// Pega o tamanho do arquivo remoto
$tamanhoarquivo = $filesize;

//
$medidas = array('KB', 'MB', 'GB', 'TB');

/* Se for menor que 1KB arredonda para 1KB */
if($tamanhoarquivo < 999){
$tamanhoarquivo = 1024;
}

for ($i = 0; $tamanhoarquivo > 999; $i++){
$tamanhoarquivo /= 1024;
}

return round($tamanhoarquivo) . $medidas[$i - 1];

Switch to this:

    function filesize_formatted($path)
    {
        $size = filesize($path);
        $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $power = $size > 0 ? floor(log($size, 1024)) : 0;
        return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
    }

    return filesize_formatted( $filesize);
    
21.08.2015 / 12:16
0

I think I've come up with a solution and I leave the code here for anyone who wants to use it. Thanks in advance to Daniel Gomes for the guidelines in the comments.

If someone has a more simplified or different way, feel free to put your answer because this will help me in my studies in php.

<?php 


    // Função que verifica o tamanho do arquivo remoto via url

    function remotefileSize($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_exec($ch);
    $filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);

    // Pega o tamanho do arquivo remoto
    $tamanhoarquivo = $filesize;

    //
    $medidas = array('KB', 'MB', 'GB', 'TB');

    /* Se for menor que 1KB arredonda para 1KB */
    if($tamanhoarquivo < 999){
    $tamanhoarquivo = 1024;
    }

    for ($i = 0; $tamanhoarquivo > 999; $i++){
    $tamanhoarquivo /= 1024;
    }

    return round($tamanhoarquivo) . $medidas[$i - 1];
    }

    echo 'O tamamnho do arquivo da url http://thumb.mais.uol.com.br/15233302-medium.jpg e '.remotefileSize('http://thumb.mais.uol.com.br/15233302-medium.jpg');



?>  
    
21.08.2015 / 09:27
0

If the current result was returning in bytes it was only dividing the result by 1048576 and you would have in MB

$tamanhoarquivo = $filesize;     /*Retorna o tamanho do arquivo em bytes*/

Logo:

$tamanhoarquivo = $filesize/1024;     /*Retorna o tamanho do arquivo em kbytes*/

$tamanhoarquivo = $filesize/1048576;  /*Retorna o tamanho do arquivo em Megabytes*/
    
21.08.2015 / 13:08