Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:47:42+00:00 2026-06-07T12:47:42+00:00

Using PHP, what’s an easy way to display file sizes with the appropriate denomination

  • 0

Using PHP, what’s an easy way to display file sizes with the appropriate denomination in regard to the amount of bytes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:47:45+00:00Added an answer on June 7, 2026 at 12:47 pm

    I came up with a little function for this:

    function filesize_exact($iSize, $iPrecision = 2, $bSpace = true) {
    
        // By Gabriel Nahmias (gabriel@terrasoftlabs.com)
    
        // Measure certain byte ranges in descending order.
    
        $sUnit = "bytes";
    
        if ($iSize > 1024 * 1024 * 1024 * 1024 * 1024) {
            $sUnit = "PB";
            $iSize /= 1024 * 1024 * 1024 * 1024 * 1024;
        } else if ($iSize > 1024 * 1024 * 1024 * 1024) {
            $sUnit = "TB";
            $iSize /= 1024 * 1024 * 1024 * 1024;
        } else if ($iSize > 1024 * 1024 * 1024) {
            $sUnit = "GB";
            $iSize /= 1024 * 1024 * 1024;
        } else if ($iSize > 1024 * 1024) {
            $sUnit = "MB";
            $iSize /= 1024 * 1024;
        } else if ($iSize > 1024) {
            $sUnit = "KB";
            $iSize /= 1024;
        }
    
        if ($iPrecision > 0 && $iPrecision != NULL)
            $iSize = round($iSize, $iPrecision);
    
        // Make sure bytes is always spaced out; could be changed.
    
        return $iSize . ($bSpace || $sUnit == "bytes" ? " " : "") . $sUnit;
    
    }
    

    Works for me. Has a couple of nice options. You could extend it to encapsulate any range of sizes so long as it’s in descending order because that’s how the logic is set up here and it’s suitable. See http://www.webopedia.com/quick_ref/FileSizeConversionTable.asp for a decent list of file size denominations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using php I am trying to get some data from a .csv file which
Using php-sdk I obtain pages in this way: $array = $this->instance->api( '/' . $fb_user
Using PHP, How to identify that the string is a path to any file?
Using PHP, Perl, or Python (preferably PHP), I need a way to query an
Using php I'm trying to figure out the most efficient way to design my
Using PHP, is there a nice way to get the (parsed) introduction only from
Using php, is there a way to redirect to an existing html page with
Using PHP, is there a way to test if the browser is accessing a
Using PHP, what's the fastest way to convert a string like this: 123 to
Using PHP to extract data from SQL and then creating a .csv file on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.