How do you filesize of an image from DB?

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

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you filesize of an image from DB?

Post by simonmlewis »

Code: Select all

$image = "/images/pages/$row->image";
echo filesize("$image");
This is producing nothing.
If I do:

Code: Select all

echo filesize("index.php");
at the top of the page, it works, but it does it in bytes.

I need to show the KB size of a file on the fly.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you filesize of an image from DB?

Post by Celauran »

filesize returns false on error, which will not produce output on echo. Make sure error reporting is on, try var_dump instead of echo until you can isolate the problem, check file_exists before checking filesize. As for output, 1 KB = 1024 bytes, so simple division should suffice.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

Try var_dump? Sorry how do you mean, in regards to this??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do you filesize of an image from DB?

Post by AbraCadaver »

Code: Select all

$image = "/images/pages/$row->image";
var_dump($row->image);
var_dump(filesize($image));
You probably need:

Code: Select all

$image = "/images/pages/{$row->image}";
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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

That produces:

[text]string(49) "3725577262black.jpg" bool(false) [/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How do you filesize of an image from DB?

Post by Eric! »

If filesize returns false then it didn't find the file. You need the full filesystem pathname, not the base URL path.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

As in the full URL, or using some form of ROOT script?
Full URL doesn't work.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

Code: Select all

			$image = $_SERVER['DOCUMENT_ROOT']."/images/pages/{$row->image}";

echo filesize("$image"));
$size = (filesize("$image")
This works. How do you on-the-fly convert it to KB ? $size = $filesize / 1024; ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

Code: Select all

			
			$image = $_SERVER['DOCUMENT_ROOT']."/images/pages/{$row->image}";

$size = (filesize("$image"));
$newsize = $size / 1024;
echo round($newsize);
Tried this. The image is actually just over 34kb, and this makes it 32kb.
It's close. I guess a more accurate measure means much larger code.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How do you filesize of an image from DB?

Post by Eric! »

Do you understand why the URL doesn't work? You are not going though the apache server when using the file tools, you are going through the file system. Take a look at the difference in the paths.

And yes, divide it. I would guess that the 34Kb number is really not kb but just the filesize divided by 1000.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

No it's the fize size / 1024. Unless PHP is lying to me?
Am I doing it right then ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How do you filesize of an image from DB?

Post by Eric! »

Have you thought of switching to decaf?

You said you thought the result should be 34kb but you get 32kb? Where did you get that is should be 34kb? Some tools don't use 1024 to convert.

If it is exactly 34kb (i.e. 34.000kb) it should be 34816 bytes.
If you are getting exactly 32kb (i.e. 32.000kb) then it should be 32768 bytes.

What is the raw filesize?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

It's actually 35,681 bytes.
I thought 34 as I saw 34 on screen.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How do you filesize of an image from DB?

Post by Eric! »

And you get 32kb from filesize/1024? Or were you just confused? Because you've confused me.

Seriously, think about the decaf. :)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you filesize of an image from DB?

Post by simonmlewis »

huh?
The script states 32KB, and yet it is 35kb. Is this because the php script is not entirely accurate?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply