I have a script that calculates file sizes per file extension. The first part is the sum of all files in that extension, the second part is the average size (deprived from the total number of files in that extension and the total size).
The problems I am having is converting the bytes into more practical units (the biggest whole unit).
The $file_size works perfectly! The output looks great. A sample of the output is:
The $avg_size which uses the exact same idea and logic as $file_size does NOT work for some reason! This is what I need help debugging.
The same output as above but with the avg_size:
As you can see, for txt it says the average size is 0.05 KB. If infact this is correct, it's not a whole unit so I want it pushed back into bytes.
If it's like .9 MB or .05 GB or whatever, I need it pushed back into XX KB and XX MB and such.
Any idea how I can fix this?
The source code:
Thanks!
The problems I am having is converting the bytes into more practical units (the biggest whole unit).
The $file_size works perfectly! The output looks great. A sample of the output is:
Code:
Extension Number of files Size of extension
txt 7158 46.74 MB
AVG 31 87.80 KB
tlx 38 624.77 KB
swf 361 25.70 MB
The $avg_size which uses the exact same idea and logic as $file_size does NOT work for some reason! This is what I need help debugging.
The same output as above but with the avg_size:
Code:
Extension Number of files Size of extension Average size
txt 7158 46.74 MB 0.05 KB
AVG 31 87.80 KB 0.09 KB
tlx 38 624.77 KB 0.61 KB
swf 361 25.70 MB 0.03 KB
As you can see, for txt it says the average size is 0.05 KB. If infact this is correct, it's not a whole unit so I want it pushed back into bytes.
If it's like .9 MB or .05 GB or whatever, I need it pushed back into XX KB and XX MB and such.
Any idea how I can fix this?
The source code:
Code:
if ($file_size < 1024)
{
print FILE "<td>$file_size bytes</td>";
}
elsif ($file_size >= 1024 && $file_size < 1024 * 1024)
{
$file_size = $file_size / 1024;
$file_size = sprintf("%.2f",$file_size);
print FILE "<td>$file_size KB</td>";
}
elsif ($file_size >= 1024 * 1024 && $file_size < 1024 * 1024 * 1024)
{
$file_size = $file_size / (1024 * 1024);
$file_size = sprintf("%.2f",$file_size);
print FILE "<td>$file_size MB</td>";
}
elsif ($file_size >= 1024 * 1024 * 1024)
{
$file_size = $file_size / (1024 * 1024 * 1024) ;
$file_size = sprintf("%.2f",$file_size);
print FILE "<td>$file_size GB</td>";
}
#########################################################
if ($avg_size < 1024)
{
print FILE "<td>$avg_size bytes</td>";
}
elsif ($avg_size >= 1024 && $avg_size < 1024 * 1024)
{
$avg_size = $file_size / 1024;
$avg_size = sprintf("%.2f",$avg_size);
print FILE "<td>$avg_size KB</td>";
}
elsif ($avg_size >= 1024 * 1024 && $avg_size < 1024 * 1024 * 1024)
{
$avg_size = $avg_size / (1024 * 1024);
$avg_size = sprintf("%.2f",$avg_size);
print FILE "<td>$avg_size MB</td>";
}
elsif ($avg_size >= 1024 * 1024 * 1024)
{
$avg_size = $avg_size / (1024 * 1024 * 1024);
$avg_size = sprintf("%.2f",$avg_size);
print FILE "<td>$avg_size GB</td>";
}
Thanks!