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> | size: $file_kb KB | 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
Need help, kinda lost
Moderator: General Moderators
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: Need help, kinda lost
The round function works ok for me.
However, although not totally related to your problem, I'm not too sure about the logic in your own code:
But perhaps there is a reason I'm not seeing. Also, your print statement looks like it could do with a little tidying up.
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.22Code: 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?
}- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Need help, kinda lost
What does this give you:
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo $file;
$filesize = filesize($file);
echo $filesize;mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Need help, kinda lost
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
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
Thank you so much for all your help.
Re: Need help, kinda lost
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! 