Jump to content

[SOLVED] Can someone PLEASE help me debug this


HaLo2FrEeEk

Recommended Posts

I'm trying to write a very simple, recursive filesize function that will convert a filesize to it's most readable format starting with bytes, meaning if it is smaller than 1024, it shows in bytes, if it is bigger than 1024, it's kb's, it then loops back to check if it is still bigger than 1024, if it is, it goes to mb's, and then if it is still bigger, it goes to gb's.  The problem is, it isn't seeing my array, or even my recurring variable that is supposed to increase after every pass, so it is not appending the proper value.  It only shows the filesize and not the bytes, kilobytes, etc.

 

Please help me debug, I can't figure out what's wrong with it.

 

<?php
$size = filesize('./Heart of Revenge_LoRes.wmv');
$byte = 0;
$byte_arr = array("bytes", "kilobytes", "megabytes", "gigabytes");
byteconv($size);

function byteconv($size) {
  if($size > 1024) {
    $size = round(($size / 1024), 2);
    $byte++;
    byteconv($size);
    } else {
    echo $size."".$byte_arr[$byte];
    }
  }
?>

Edit: 2 other posts, but I typed it out, so it's getting posted!

 

You could do something like this:

 


$size = filesize('./Heart of Revenge_LoRes.wmv');

$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'Umm your file is way too big');

if($size > 1024) {
$s = $size/1024;
$s2 = (int) $s2;
}
else {
$s = $size;
$s2 = 0;
}

echo 'The file is '.$s.$s2';

what mkoga meant was this:

 

<?php
$size = filesize('./Heart of Revenge_LoRes.wmv');
$byte = 0;
$byte_arr = array("bytes", "kilobytes", "megabytes", "gigabytes");
byteconv($size);

function byteconv($size) {
global $byte, $byte_arr; //made the variables GLOBAL so the function could access them.

  if($size > 1024) {
    $size = round(($size / 1024), 2);
    $byte++;
    byteconv($size);
    } else {
    echo $size."".$byte_arr[$byte];
    }
  }
?>

 

because those variables wernt accisble, unless they were passed to the function or made global

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.