HaLo2FrEeEk Posted June 28, 2007 Share Posted June 28, 2007 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]; } } ?> Quote Link to comment Share on other sites More sharing options...
mkoga Posted June 28, 2007 Share Posted June 28, 2007 I believe you have to specify the scope with the global keyword. <?php $myVar = array(); function doSomething() { global $myVar; $myVar[] = 'do something'; } ?> Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted June 28, 2007 Author Share Posted June 28, 2007 Uhm, can you put that into the context of my code please, and I don't know, I think globals are disabled for security on my system. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 28, 2007 Share Posted June 28, 2007 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'; Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 28, 2007 Share Posted June 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted June 28, 2007 Author Share Posted June 28, 2007 OMG, thank you all, I knew it was something stupid like that. I actually tried passing $byte to hte function, but it still didn't work, I didn't think of passing them both. Thank you! Quote Link to comment Share on other sites More sharing options...
corbin Posted June 28, 2007 Share Posted June 28, 2007 On a random side note: My code posted was riddled with tired logic.... It doesn't work lol. Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 28, 2007 Share Posted June 28, 2007 what about using range example and do valadation to file size. <?php $bytes=range(0,1024); $kilo_bytes=range(102.40,10.24); $mega_bytes=range(10.24,1.024); $giga_bytes=range(1024,102.4); $tiga_bytes=range(102.4,10.24); ?> Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted June 28, 2007 Author Share Posted June 28, 2007 I think a recurring function is MUCH more efficient than writing a lot of code. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.