Click here to Skip to main content
Click here to Skip to main content

A simple class in PHP to calculate the size of file

By , 5 Feb 2013
 

Introduction 

It's a very useful source written in PHP to calculate file size. Two ways have been implemented here:

  1. by Unit Type (e.g.: B, KB, MB, ....)
  2. by precision (e.g.: digits after decimal)

Source Files

  1. class.file_management.php - the main class file
  2. index.php - homepage
  3. test.test - file used as an example only  

Using the code

Class declaration is as follows:

include("class.file_management.php");
$objZF    =    new FileManagement;

Now call the function as follows:

echo $objZF->file_size_option("test.test", "KB");

In the above way, you can get your result in your desired unit, bytes, kilobytes, megabytes, whatever you want. And the below example is of an auto-fixed unit system, the function will decide the return type of unit. But here you can set the precision, i.e., the number of digits after the decimal.

echo $objZF->file_size_auto("test.test", 2);

The complete class definition is listed here:

class FileManagement
{
    public $units    =    array('B', 'KB', 'MB', 'GB', 'TB');

    function file_size_auto($fileName, $precision) {
    
        $bytes    =    filesize($fileName);
        
        $bytes    =    max($bytes, 0);
        $pow    =    floor(($bytes ? log($bytes) : 0) / log(1024));
        $pow     =    min($pow, count($this->units) - 1);
      
        $bytes /= pow(1024, $pow);
      
        $retValue = round($bytes, $precision) . ' ' . $this->units[$pow];
        return $retValue;
    }
    
    function file_size_option($fileName, $unitType) {
        
        switch($unitType) {
            case $this->units[0]: 
                $fileSize = number_format((filesize(trim($fileName))), 1) ; 
                break;
            case $this->units[1]: 
                $fileSize = number_format((filesize(trim($fileName))/1024), 1) ; 
                break;
            case $this->units[2]: 
                $fileSize = number_format((filesize(trim($fileName))/1024/1024), 1) ; 
                break;
            case $this->units[3]: 
                $fileSize = number_format((filesize(trim($fileName))/1024/1024/1024), 1) ; 
                break;
            case $this->units[4]: 
                $fileSize = number_format((filesize(trim($fileName))/1024/1024/1024/1024), 1) ; 
                break;
        }
        
        $retValue = $fileSize. ' ' .$unitType;
        return $retValue;
    }
}

History

Initial post: 13th January, 2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Zamshed Farhan
Web Developer
Bangladesh Bangladesh
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMissing source zipstaffSmitha Vijayan5 Feb '13 - 7:38 
GeneralMy vote of 5groupikaaes13 Jan '13 - 11:19 
GeneralRe: My vote of 5memberSMZF8013 Jan '13 - 15:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 5 Feb 2013
Article Copyright 2013 by Zamshed Farhan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid