Page 1 of 2

How do you filesize of an image from DB?

Posted: Wed Sep 18, 2013 11:03 am
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.

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

Posted: Wed Sep 18, 2013 11:27 am
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.

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

Posted: Wed Sep 18, 2013 12:34 pm
by simonmlewis
Try var_dump? Sorry how do you mean, in regards to this??

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

Posted: Wed Sep 18, 2013 12:46 pm
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}";

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

Posted: Wed Sep 18, 2013 12:52 pm
by simonmlewis
That produces:

[text]string(49) "3725577262black.jpg" bool(false) [/text]

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

Posted: Wed Sep 18, 2013 1:05 pm
by Eric!
If filesize returns false then it didn't find the file. You need the full filesystem pathname, not the base URL path.

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

Posted: Wed Sep 18, 2013 1:09 pm
by simonmlewis
As in the full URL, or using some form of ROOT script?
Full URL doesn't work.

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

Posted: Wed Sep 18, 2013 1:14 pm
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; ??

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

Posted: Wed Sep 18, 2013 1:17 pm
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.

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

Posted: Wed Sep 18, 2013 1:27 pm
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.

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

Posted: Wed Sep 18, 2013 1:29 pm
by simonmlewis
No it's the fize size / 1024. Unless PHP is lying to me?
Am I doing it right then ??

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

Posted: Wed Sep 18, 2013 1:36 pm
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?

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

Posted: Wed Sep 18, 2013 1:40 pm
by simonmlewis
It's actually 35,681 bytes.
I thought 34 as I saw 34 on screen.

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

Posted: Wed Sep 18, 2013 1:54 pm
by Eric!
And you get 32kb from filesize/1024? Or were you just confused? Because you've confused me.

Seriously, think about the decaf. :)

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

Posted: Wed Sep 18, 2013 1:56 pm
by simonmlewis
huh?
The script states 32KB, and yet it is 35kb. Is this because the php script is not entirely accurate?