Wrong file size

Asked

Viewed 149 times

2

Hello, I wonder how I can fix the following error: I have the code below, which gives the file size, consulting inside the host directory.

However, let’s say that the file has 800Mbs, then what happens is that it shows a value a little lower, as 792Mbs, not its exact value. I would like to know how to fix this error in the code below, I appreciate 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');

    ?>

Update 18/05/2015: I’m having a problem with this type code I have a dedicated and I used this function to show the file size and add the value in SQL in my dedicated worked only that in my friend’s dedicated keeps giving error in line 2 in $filesize = filesize($file);

Keeps giving the following warning Warning: filesize() [Function.filesize]: stat failed for .. /driver/rar/Document1.rar in /home/meusite/public_html/Adm/additor.php on line 49 line 49 in the case and where this function above starts and integrates to my item addition code.

Obs: line 49 and $filesize = filesize($file);

Could someone help me fix this?

The Bruno already helped me fix the code to put the exact size with everything I don’t understand why I’m giving this error.

  • How did you get the "real" file size information? It can be a simple difference between kB and kb usage.

  • You’re making a mistake or a warning?

  • Normally this error/warning may occur due to a lack of read/write permissions in the directory where the files are. You can check the permissions in the file/directory?

  • in case and warning the code is not wrong.

1 answer

4


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

 $tamanhoarquivo /= 1024;

But yes

 $tamanhoarquivo /= 1000;

An important note from the function manual (may not apply in your case 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.

  • 1

    Thanks for the help now this putting the right size.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.