PHP file size in KB

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
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

PHP file size in KB

Post by tomsace »

Hey,
I want to show the file size of images on my website. I can make it work to show the file size in bytes but not kb.
This is my script I am trying to work with so far..

Code: Select all

<?php $file = 'FILE.JPG'; $file_kb = round(($file / 1024), 2); echo"$file_kb" ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP file size in KB

Post by requinix »

Ever heard of filesize?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP file size in KB

Post by tomsace »

Yes but that script shows you the file size in bytes which I said I can already do.
I want the results to be shown in kilo bytes.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP file size in KB

Post by requinix »

tomsace wrote:Yes but that script shows you the file size in bytes which I said I can already do.
I want the results to be shown in kilo bytes.
No, it doesn't, it shows you the number 0.

Here's the weird thing. Your code demonstrates exactly what you want to do, yet you haven't realized it. And you say you have code to get it in bytes yet that's not the code you posted.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re

Post by McInfo »

tomsace wrote:

Code: Select all

<?php $file = 'FILE.JPG'; $file_kb = round(($file / 1024), 2); echo"$file_kb" ?>
In your code, $file is a string, not a number.

The code you posted is equivalent to this:

Code: Select all

<?php echo round(('FILE.JPG' / 1024), 2); ?>
Instead,

Code: Select all

<?php
if ($size = filesize('FILE.JPG'))
{
    echo round(($size / 1024), 2).' KB';
}
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 3:21 pm, edited 1 time in total.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP file size in KB

Post by tomsace »

Thanks McInfo for your help.
The code works perfect. I am just trying to get the filename to read from the mysql database.

Code: Select all

<?php
if ($size = filesize('images/ECHO NAME.jpg'))
{
    echo round(($size / 1024), 2).' KB';
}
?>
I have tried adding $line[idname] and echo"$line[idname]" and even the whole thing <?php echo"$line[idname]" ?>
What I am trying to ask is how can I echo the filename into this code?
Last edited by tomsace on Sun Apr 19, 2009 6:07 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP file size in KB

Post by McInfo »

Don't use quotes around variables by themselves. Do use single-quotes around array string indexes.

Code: Select all

echo $line['idname'];
PHP Manual: Array do's and don'ts (From twigletmac's signature)

In the context of the example,

Code: Select all

<?php
if ($size = filesize($line['idname']))
{
    echo round(($size / 1024), 2).' KB';
}
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 3:22 pm, edited 1 time in total.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP file size in KB

Post by tomsace »

Thanks,
Is there a way I can add a folder? For example you gave me the code:

Code: Select all

<?php
if ($size = filesize($line['idname']))
{
    echo round(($size / 1024), 2).' KB';
}
?>
I need to something like:

Code: Select all

<?php
if ($size = filesize(images/$line['idname'].jpg))
{
    echo round(($size / 1024), 2).' KB';
}
?>
I need to add the 'idname' with the images/ before and .jpg after. Can't get anything like this working.
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: PHP file size in KB

Post by Pulni4kiya »

Try this:

Code: Select all

<?php
if ($size = filesize('images/' . $line['idname'] . '.jpg'))
{
    echo round(($size / 1024), 2).' KB';
}
?>
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP file size in KB

Post by tomsace »

Oh I see, thanks alot works perfect!
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP file size in KB

Post by tomsace »

Hi,
Its not to do with the file size in kb but its do do with last answer.

If I wanted to echo a result in a query how would I do this?
This is my code:

Code: Select all

$query = 'SELECT * FROM games WHERE category = "ECHO CATEGORY HERE" ORDER BY rand() DESC limit 6';
I Had a go and thought of something like this but couldn't get it..

Code: Select all

$query = 'SELECT * FROM games WHERE category = "'echo' . $line[category]" ORDER BY rand() DESC limit 6';
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP file size in KB

Post by McInfo »

It's time to study.

PHP Manual: Language Reference

In particular, Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 3:23 pm, edited 1 time in total.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP file size in KB

Post by tomsace »

Can't figure out which script to use on mine?
Can you give me a clue? Phone a friend?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP file size in KB

Post by tomsace »

Did it using this method:

Code: Select all

$cat = "$line[category]";
$query = "SELECT * FROM games WHERE category = '$cat' ORDER BY rand() DESC limit 6";
Post Reply