Page 1 of 1

Need help, kinda lost

Posted: Wed Nov 17, 2010 1:15 pm
by danwguy
Ok so I have a page that looks in a directory and lists all files in that directory as links. Everything is working great except one thing, the filesize. I can get the file size in byte with $filesize = filesize($file) and then printing it with the rest of the code but here's what I have right now.
$filesize = filesize($file);
if($filesize < 1048576) {
$file_kb = round($filesize / 1024, 2);
}
else {
$file_kb = $filesize;
}

and then it prints on the page like this...
print "<a href=$temp>$file</a> &nbsp;|&nbsp; size: $file_kb KB &nbsp;|&nbsp; type: ".$ext." <img src=\"../images/icon-document.png\" width=\"13\" height=\"18\" alt=\"Document\" title=\"Document\" />
</li>";
but it always shows up as size: 0 kb I can't figure out why it wont give me the correct filesize. Like I said if I just do $filezise = filesize($file) then in the print do size: $filesize KB it shows in bytes, but I need it in kb. please help. thank you

Re: Need help, kinda lost

Posted: Wed Nov 17, 2010 1:56 pm
by greyhoundcode
The round function works ok for me.

Code: Select all

$bytes  = filesize('/home/user1/Documents/JS Cheatsheet.odt');
$kbytes = round($bytes/1024, 2);

echo "<p>$bytes</p>";  // In my case = 19682
echo "<p>$kbytes</p>"; // Correctly shows 19.22
However, although not totally related to your problem, I'm not too sure about the logic in your own code:

Code: Select all

$filesize = filesize($file);

if($filesize < 1048576) {
    $file_kb = round($filesize / 1024, 2);
}
else {
    $file_kb = $filesize; // Why aren't you dividing in this case?
}
But perhaps there is a reason I'm not seeing. Also, your print statement looks like it could do with a little tidying up.

Re: Need help, kinda lost

Posted: Wed Nov 17, 2010 2:51 pm
by AbraCadaver
What does this give you:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');

echo $file;
$filesize = filesize($file);
echo $filesize;

Re: Need help, kinda lost

Posted: Wed Nov 17, 2010 3:29 pm
by danwguy
Figured it out. I was giving that command before looping through the directory to get the whole directory list. I put it in after the loop and it works great. One more little thing though, is there a way to display the file size as 224kb and not 224.07kb? Also how do you mean clean up the print statement?

Re: Need help, kinda lost

Posted: Wed Nov 17, 2010 4:00 pm
by s992

Code: Select all

$file_kb = round($filesize / 1024, 2); // Round to two decimal points
$file_kb = round($filesize / 1024); // Round to the nearest whole number

Re: Need help, kinda lost

Posted: Wed Nov 17, 2010 4:53 pm
by danwguy
Thank you so much for all your help.

Re: Need help, kinda lost

Posted: Wed Nov 17, 2010 8:09 pm
by Luke
Please use more descriptive post titles. "Need help" doesn't give prospective helpers anything to go by. You will have much better luck getting help if you provide a concise, but descriptive post title. Just a little advice from somebody who's been around a while. Thanks! :)