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];
    }
  }
?>

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.