Click here to Skip to main content
14,693,352 members
Articles » Web Development » PHP » General
Posted 13 Jan 2013

Tagged as


1 bookmarked

A simple class in PHP to calculate the size of file

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
5 Feb 2013CPOL
A simple class in PHP to calculate the weight of file


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:

$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) ; 
            case $this->units[1]: 
                $fileSize = number_format((filesize(trim($fileName))/1024), 1) ; 
            case $this->units[2]: 
                $fileSize = number_format((filesize(trim($fileName))/1024/1024), 1) ; 
            case $this->units[3]: 
                $fileSize = number_format((filesize(trim($fileName))/1024/1024/1024), 1) ; 
            case $this->units[4]: 
                $fileSize = number_format((filesize(trim($fileName))/1024/1024/1024/1024), 1) ; 
        $retValue = $fileSize. ' ' .$unitType;
        return $retValue;


Initial post: 13th January, 2013


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


About the Author

Zamshed Farhan
Software Developer
Bangladesh Bangladesh
I have received B.Sc. degree in Computer Science and Engineering from Khulna University of Engineering and Technology, (KUET) Bangladesh in 2003 and Master of Business Administration in Management from University of Dhaka, Bangladesh in 2009. I started my IT career as a Software Engineer at BDCOM Online Limited, then worked for BRAC BDMail Network Limited (bracNet). At present, I am working with Application Development, Integration and Maintenance department of the International Centre for Diarrhoeal Disease Research, Bangladesh (ICDDR,B) - an international health research organization located in Dhaka, Bangladesh, dedicated to saving lives through research and treatment.

Comments and Discussions

QuestionMissing source zip Pin
Smitha Nishant5-Feb-13 8:38
protectorSmitha Nishant5-Feb-13 8:38 
GeneralMy vote of 5 Pin
ikaaes13-Jan-13 12:19
Memberikaaes13-Jan-13 12:19 
GeneralRe: My vote of 5 Pin
Zamshed Farhan13-Jan-13 16:38
MemberZamshed Farhan13-Jan-13 16:38 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.