Page 1 of 1

PHP file size in KB

Posted: Sat Apr 18, 2009 4:59 pm
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" ?>

Re: PHP file size in KB

Posted: Sat Apr 18, 2009 5:13 pm
by requinix
Ever heard of filesize?

Re: PHP file size in KB

Posted: Sat Apr 18, 2009 5:21 pm
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.

Re: PHP file size in KB

Posted: Sat Apr 18, 2009 8:36 pm
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.

Re

Posted: Sat Apr 18, 2009 10:25 pm
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.

Re: PHP file size in KB

Posted: Sun Apr 19, 2009 3:57 am
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?

Re: PHP file size in KB

Posted: Sun Apr 19, 2009 11:50 am
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.

Re: PHP file size in KB

Posted: Sun Apr 19, 2009 6:08 pm
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.

Re: PHP file size in KB

Posted: Sun Apr 19, 2009 7:01 pm
by Pulni4kiya
Try this:

Code: Select all

<?php
if ($size = filesize('images/' . $line['idname'] . '.jpg'))
{
    echo round(($size / 1024), 2).' KB';
}
?>

Re: PHP file size in KB

Posted: Sun Apr 19, 2009 7:12 pm
by tomsace
Oh I see, thanks alot works perfect!

Re: PHP file size in KB

Posted: Mon Apr 20, 2009 11:40 am
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';

Re: PHP file size in KB

Posted: Mon Apr 20, 2009 12:03 pm
by McInfo
It's time to study.

PHP Manual: Language Reference

In particular, Edit: This post was recovered from search engine cache.

Re: PHP file size in KB

Posted: Tue Apr 21, 2009 4:57 pm
by tomsace
Can't figure out which script to use on mine?
Can you give me a clue? Phone a friend?

Re: PHP file size in KB

Posted: Wed Apr 22, 2009 5:08 pm
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";