How do I check the filesize of a stored image?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I check the filesize of a stored image?

Post by simonmlewis »

I need to add up all the images we have on a homepage, from the admin screen.
To do that, I want to get the filesize of each "banner" type image, and then at the bottom, show the filesize overall.

Sounds easy, but it won't let me even get one file size.

I can do it when I run an upload with the ['tmp'] format, but I want to get the filesize like this:

Code: Select all

$path = "/images/pages/$row->image";
echo filesize("$path");
And all I get is this error:

[text]Warning: filesize(): stat failed for /images/pages/97242645m27g.jpg in C:\xampp\phpMyAdmin\site-wide\includes\a_home.inc on line 767[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I check the filesize of a stored image?

Post by Christopher »

Are you sure your file path is right. Usually /images is in the document root directory.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check the filesize of a stored image?

Post by simonmlewis »

If I put that string into a <img> tag, I get the image.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I check the filesize of a stored image?

Post by Christopher »

That's because it's the path relative to the domain name in the URL. The filesize() function wants the absolute path in the file system of the server. Those are two very different things. It should be $path = "/path/to/document/root/images/pages/{$row->image}";
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check the filesize of a stored image?

Post by simonmlewis »

Oh ok.
So how do you test it on a Windows PC, and i guess on a server I need to find out what the "path" is.
Or is there a PHP script that can help me identify that?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I check the filesize of a stored image?

Post by Christopher »

It's document root, so php.ini or use phpinfo() ?
(#10850)
Post Reply