Wrong file size

2
Hello, I would like to know how I can fix the following error: I have the code below, which gives the file size, querying inside the host directory.

However, let's say the file has 800Mbs, so what happens is that it shows a value a little lower, like 792Mbs, not its exact value. I would like to know how I can fix this error in the code below, since I already thank the help of all.

    <?php

    function tamanho_arquivo($arquivo) {
        $tamanhoarquivo = filesize($arquivo);

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

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

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

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

// tamanho do arquivo 800Mbs
    echo tamanho_arquivo('filme.avi');

    ?>
I have a problem with this type code I have a dedicated and I used this function to show the size of the file and add the value in the SQL in my dedicated worked only that my friend's dedicated one is giving error on line 2 in $ filefile = filesize ($ file);

Warning: filesize () [function.filesize]: stat failed for ../driver/rar/documento1.rar in /home/meusite/public_html/adm/additional.php on line 49 line 49 in case and where this function above starts and integrates with my item adding code.

Note: line 49 and $ filefile = filesize ($ file);

Could anyone help me fix this?

Bruno has already helped me to fix the code to put the exact size with everything I am not intending the reason to be giving this error.

    
asked by anonymous 04.05.2015 / 06:14

1 answer

4

The filesize function returns the size of a file in bytes, so you should not do

 $tamanhoarquivo /= 1024;

But yes

 $tamanhoarquivo /= 1000;

An important note in the function manual (this may not apply to you, as it depends on the architecture):

  

Note: Because PHP's integer type is signed and many platforms use   32bit integers, some filesystem functions may return unexpected   results for files which are larger than 2GB.

    
04.05.2015 / 10:05