-2

After thinking mine was in error,

I found LOT AT LOTS of scripts the do this: https://gist.github.com/liunian/9338301

And there are several here at S.O. I used, but had the same annoying "B" as a size.

This issue seemed to rear it's ugly head when I switched to php v7.xxx First issues is I have to typcase a floated number (or double) or else I get a "A non well formed numeric value encountered" After some research, apparently this is NOT a bug. At least that is how I read it.

So after typcasting it, the error goes away but the value returned is always a "B' filesize = 87.5B (when it should be MB or GB).

I am pretty sure Javascript will work, but would rather keep it with php.

Thanks for looking

current live script that is producing a "B" only

 public function readableBytes($size,  $type='pc') { //ignore the pc - it is for something else - disabled for debugging
        $size = (double)$size;
        static $units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
        $step = 1024;
        $i = 0;
        while (($size / $step) > 0.9) {
            $size = $size / $step;
            $i++;   
        }
        return round($size, 2).$units[$i];
    }// function readbbleBytes
Jake Stone
  • 95
  • 10
  • 2
    I've tried it and it seems to be able to produce other things - `echo readableBytes(1000000);` -> `0.95MB` – Nigel Ren Nov 22 '18 at 20:06
  • Which version of PHP are you using, and on which OS (including wordsize - 32, or 64 bit), and what is shown if you `var_dump($size);` on the first line of the function? – Alister Bulman Nov 22 '18 at 20:08
  • 3
    Can't reproduce here. What is your input `$size` data? – Felippe Duarte Nov 22 '18 at 20:16
  • 1
    Working fine for me as well – miken32 Nov 22 '18 at 20:26
  • proof: https://3v4l.org/dOQkX – But those new buttons though.. Nov 22 '18 at 20:30
  • not sure why the negative - but o well. PHP v7.2.11 64bit, on windows using xampp. var dump: string(8) "91485852" - that explains the typcast I need(one answer resolved). however it should be read as a 91485852, never had to typcast a string in the past, PHP would adopt the "string" as a "integer/real/double etc" as per usage. Using the above var, it produces the following: (87.25B)<-- note the B. BTW not interested if it is working for people. I KNOW IT WORKS, I am asking as to why mine is not. Keep in mind, this is an old script that USED TO WORK, I may need to update the whole site – Jake Stone Nov 22 '18 at 21:02
  • fixed to var dum = float(91485852) . but still producing the B as a readable figure. – Jake Stone Nov 22 '18 at 21:09
  • http://sandbox.onlinephpfunctions.com/code/b2db73cb11f7fd2cfadbaaa102285bfff2f5c65f – But those new buttons though.. Nov 22 '18 at 21:36
  • OK after some grueling search, it is NOT the script - I did a die() on it, and it produced the desired results. everything that is posting the results is accurate as well, so will have to keep digging. every die() spot is showing correct, however end result: `code`
    (87.25 B)
    – Jake Stone Nov 22 '18 at 21:38
  • Hey look on the bright side, you're learning the basics of how to debug your code. Nothing beats first hand experience and figuring things out for yourself – But those new buttons though.. Nov 22 '18 at 21:40

2 Answers2

0

This is a unique issue to those that use a loop for assigning (custom)template tags with an array field.

The filesize is one of many fields in an array I use.

I used a loop to go through each field and assign it to a template tag. Not sure why the "B" came up. My suspicion is that the result of a "string" = 0 bytes. Even though It showed the actual size. edit: spelling & claification So to fix, in the middle of the loop, I forced the $array['filesize'] = readableBytes($array['filesize'])). Before it was loop tag = fieldname.

foreach ($arr as $field=>$data) {
$arr['filesize'] = readableBytes($array['filesize'])); // fix was put here
$page = str_ireplace("{$field}", $data, $page);
}
Jake Stone
  • 95
  • 10
-1

The following seems to work (checked in phptester):

function human_filesize($bytes, $decimals = 2)
{
  if ($bytes < 1024) {
    return $bytes . ' B';
  }

  $factor = floor(log($bytes, 1024));
  return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . ['B', 'KB', 'MB', 'GB', 'TB', 'PB'][$factor];
}

This is a cut and paste of a post by gladx in the following thread, where you'll find several optimised examples: https://gist.github.com/liunian/9338301#gistcomment-2183132

Eric Vautier
  • 139
  • 4