Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > php from hell: 143266/1024 is not 0.00150632...

php from hell: 143266/1024 is not 0.00150632...
Thread Tools
Grizzled Veteran
Join Date: Jun 2001
Status: Offline
Reply With Quote
Sep 25, 2003, 11:49 PM
 
Am I crazy? This php script will not divide properly. For me, $bytes is 143266. That's good. That's the right size. God forbid I return ($bytes/1024)...I get some number like 0.00150632..... What is that about? I've been pulling my hair out for like an hour over this!

$bytes is right, but if I do $bytes/1024, it screws up horribly... I'm concerned that $bytes somehow isn't an integer, but php supposedly takes care of it...


function dir_size($dir)
{
$totalsize=0;
if($dirstream = opendir($dir)) // opening the directory
{
while(false!==($filename=readdir($dirstream)))
{
if($filename != "." && $filename != ".." && $filename != ".DS_Store") // ignore stupid things
{ //recursion to find all files/folders
if (is_file($dir."/".$filename))
$totalsize+=filesize($dir."/".$filename);
if (is_dir($dir."/".$filename))
$totalsize+=dir_size($dir."/".$filename);
}
}
}
closedir($dirstream); // be a good script and clean up

/*********
$types = array("B","KB","MB","GB","TB");
$count = 0;
while ($totalsize > 1024)
{
$totalsize /= 1024;
$count++;
}
//return round($bytes,2)." ".$types[$current];
*********/

return $totalsize; //." ".$types[$count];
}

echo(dir_size("../library/"));
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Sep 26, 2003, 02:06 AM
 
Two things:

1. Check to see what it prints out after each division (in the loop). You may be dividing too many times.

2. Make sure the directory you're opening is the one you think you're opening.

Disclaimer: I didn't look the code over too well as I'm headed off for bed now, so take the post with a grain of an undesirable mineral.
Travis Sanderson
     
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
Sep 26, 2003, 02:16 AM
 
I'm not positive, but are you sure the problem isn't the line:

Code:
$totalsize+=dir_size($dir."/".$filename);
Basically you're passing whatever number it comes up with (in B, KB, MB, etc) and passing it off as bytes for calculating. You can't start adjusting the numbers in a function you want to use in a recursive manner like that. It must always return the same type of value so calculations are accurate.

Even worse, if you uncommented more lines, you'd be passing the string ("MB", "KB", etc) and how is it supposed to calculate with that?

I suggest one function for coming up w/ the number of bytes, and another function to take the result of that and convert it from bytes to whatever is appropriate. dir_size should ONLY return bytes... nothing more.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:12 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2