PHP function to find out the size of a file

Hello people! A few weeks ago I was developing a system where the client requested that in addition to its institutional website it also had a restricted area where he could make digital documents available to his clients. In short, each customer would have a virtual space with documents and other important files and access everything with their username / password.

Well then. When the system is accessed, all files that are available to the user are listed for that user. And this list shows the name of the document, its format, the date of submission, a brief description of what it is about and the size of the file since, obviously, the customer has the option to download it.

I had to break my head a bit to solve this file size issue The php offers a native function ( filesize ($ file) ) which returns the size of a file, but in bytes. And I wanted something that showed the value already formatted with its unit of measurement (KB, MB, GB, TB, etc.).

It was then that I implemented the following function that performs this task. Look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
 
/**
    * Returns the size of a given file in KB, MB, GB TB, etc.
    * @author Rafael Wendel Pinheiro
    * @param String $file The file to be scanned
    * @return String The file size (already formatted)
*/
function file_size($file) {
    $size = filesize($arquivo);
 
    /* Measures */
    $measures = array('KB', 'MB', 'GB', 'TB');
 
    /* If less than 1KB, rounds to 1KB */
    if($size < 999){
        $size = 1000;
    }
 
    for ($i = 0; $size > 999; $i++){
        $size /= 1024;
    }
 
    return round($size ) . $measures[$i - 1];
}

Note that I must pass as a parameter the file (path) that I want to know the size. By the native function filesize I capture the size in bytes of the file. I create an array with the units of measures (KB, MB, GB and TB – they will hardly have files above the TBs, but feel free to include other measures ). I define a rule that all files under 1KB will be rounded to 1KB in size Then I go through a loop to split the filesize return ( in bytes) to the new unit. As long as the file size is greater than 999 bytesit will be divided by 1024 (1024 bytes = 1KB; 1024KBs = 1MB and so on). Finally, I use the round function to round the value and concatenate the unit of measurement based on the value of the repetition loop index ($ i) .

To use the function is easy. Look:

1
2
3
4
5
6
 
echo file_size('foto1.jpg');
/* Returns 670KB */
 
echo file_size('musica1.mp3');
/* Returns 5MB */

And period.

I hope that this function will help you as much as it helped me.

Hugs!!!

Holds a university degree in Information Systems, a postgraduate degree in Database Systems and a master's degree in Education with a focus on Sociocommunity Technologies. He works as a professor of technical and technological education at the Federal Institute of Education, Science and Technology of São Paulo, teaching subjects in the areas of programming, database, project development and software engineering.

Posts relacionados

Leave a Reply

Your email address will not be published. Required fields are marked *