1

I have something like this

<td><?php echo $row_rsPhoto['size']; ?></td>

and displays the file size in bytes, can do that in kilobytes, I'm trying to do this - function

public function size_as_kb()
{
    if ($this->size < 1024) {
        return "{$this->size} bytes";
    } elseif ($this->size < 1048576) {
        $size_kb = round($this->size/1024);
        return "{$size_kb} KB";
    } else {
        $size_mb = round($this->size/1048576, 1);
        return "{$size_mb} MB";
    }
}

I do not know if this works and how to connect it $row_rsPhoto['size']; with function

thank you in advance for your help

Marc Audet
  • 46,011
  • 11
  • 63
  • 83
Mantykora 7
  • 173
  • 2
  • 8
  • 19
  • Your question is not completely clear. Do you simply want to return the size in kilobytes? Or do you want to return the number of bytes, kilobytes, megabytes or gigabytes, depending on which is applicable? – MC Emperor Sep 25 '13 at 11:41

5 Answers5

5

look at this discussion

PHP filesize MB/KB conversion

then you can use

<td><?php echo size_as_kb($row_rsPhoto['size']); ?></td>
Community
  • 1
  • 1
arilia
  • 9,373
  • 2
  • 20
  • 44
3

This outputs Kilo Bytes..

<td><?php echo $row_rsPhoto['size'] >> 10; ?></td>
Prasanth
  • 5,230
  • 2
  • 29
  • 61
1

You wrote your answer. Only you needed to add the parameter of the function.

public function size_as_kb($yoursize) {
  if($yoursize < 1024) {
    return "{$yoursize} bytes";
  } elseif($yoursize < 1048576) {
    $size_kb = round($yoursize/1024);
    return "{$size_kb} KB";
  } else {
    $size_mb = round($yoursize/1048576, 1);
    return "{$size_mb} MB";
  }
}

Call that by writing

$photo_size = size_as_kb($row_rsPhoto['size']);
mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40
1

use function parameters:

 echo size_as_kb($row_rsPhoto['size']);

public function size_as_kb($size=0) {
    if($size < 1024) {
    return "{$size} bytes";
    } elseif($size < 1048576) {
    $size_kb = round($size/1024);
    return "{$size_kb} KB";
    } else {
    $size_mb = round($size/1048576, 1);
    return "{$size_mb} MB";
    }
    }
timod
  • 585
  • 3
  • 13
1

just need to call the function and add parameters to it.

<td><?php echo size_as_kb($row_rsPhoto['size']); ?></td>

 public function size_as_kb($size) {
if($size < 1024) {
return "{$size} bytes";
} elseif($size < 1048576) {
$size_kb = round($size/1024);
return "{$size_kb} KB";
} else {
$size_mb = round(size/1048576, 1);
return "{$size_mb} MB";
}
}