Need help, kinda lost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Need help, kinda lost

Post 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
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Need help, kinda lost

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need help, kinda lost

Post 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;
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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Need help, kinda lost

Post 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?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Need help, kinda lost

Post 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
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Need help, kinda lost

Post by danwguy »

Thank you so much for all your help.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need help, kinda lost

Post 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! :)
Post Reply