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
tomsace
Forum Contributor
Posts: 167 Joined: Thu Jan 01, 2009 8:07 pm
Post
by tomsace » Sat Apr 18, 2009 4:59 pm
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" ?>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sat Apr 18, 2009 5:13 pm
tomsace
Forum Contributor
Posts: 167 Joined: Thu Jan 01, 2009 8:07 pm
Post
by tomsace » Sat Apr 18, 2009 5:21 pm
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sat Apr 18, 2009 8:36 pm
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.
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Sat Apr 18, 2009 10:25 pm
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
Post
by tomsace » Sun Apr 19, 2009 3:57 am
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.
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Sun Apr 19, 2009 11:50 am
Don't use quotes around variables by themselves. Do use single-quotes around array string indexes.
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
Post
by tomsace » Sun Apr 19, 2009 6:08 pm
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
Post
by Pulni4kiya » Sun Apr 19, 2009 7:01 pm
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
Post
by tomsace » Sun Apr 19, 2009 7:12 pm
Oh I see, thanks alot works perfect!
tomsace
Forum Contributor
Posts: 167 Joined: Thu Jan 01, 2009 8:07 pm
Post
by tomsace » Mon Apr 20, 2009 11:40 am
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';
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Mon Apr 20, 2009 12:03 pm
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
Post
by tomsace » Tue Apr 21, 2009 4:57 pm
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
Post
by tomsace » Wed Apr 22, 2009 5:08 pm
Did it using this method:
Code: Select all
$cat = "$line[category]";
$query = "SELECT * FROM games WHERE category = '$cat' ORDER BY rand() DESC limit 6";