php get the KB size of an image
Asked Answered
B

6

7

i've been googleing but all i get is getimagesize and filesize.
getimagesize dosent get the KB size just width and height which is not what im looking for.
filesize give me the message Warning: filesize() [function.filesize]: stat failed for
the file in question is 51kb .jpg file

$imgsize=filesize("http://localhost/projects/site/schwe/user/1/1.jpg");

does not work,

how do i accomplish this?

Bluegreen answered 27/9, 2010 at 15:20 Comment(2)
Which version of PHP are you using?Sabah
Maybe see this questionSabah
K
7

You cannot get file size of remote elements, either give a relative path on your system OR do a file_get_contents() to get contents first . Thus, instead of http:// , do a filesize('/path/to/local/system') . Make sure its readable by php process

Kitkitchen answered 27/9, 2010 at 15:23 Comment(2)
yeah, true, i just noticed, thanks. i should have thought of it before hand, im such an idiot.Bluegreen
What about a tip in filesize() docs on PHP.net? It says: "As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality". Seems, you can use filesize() remote elements. You only have to have your server and PHP parser configured properly (security matters).Hera
P
6

You can't look up the filesize of a remote file like that. It is meant for looking at the filesize of local files.

For instance...

$imgsize = filesize( '/home/projects/site/1.jpg' );
Pentathlon answered 27/9, 2010 at 15:23 Comment(1)
See my comment to Stewie's answer. You can use files size for a remote file, if only your environment and server are correctly configured.Hera
S
1

filesize() is the function to use. It might be failing because

  1. You're trying to get a web address & URL wrappers may not be turned on
  2. That URL isn't valid.

If you're trying to run filesize() on a local file, reference the file system path, not some web URL.

Syllogistic answered 27/9, 2010 at 15:25 Comment(0)
T
1

Or you can also do something like :

$header = get_headers($url);
foreach ($header as $head) {
    if (preg_match('/Content-Length: /', $head)) {
        $size = substr($head, 15);                      
    }
}
Thibaut answered 24/9, 2013 at 23:5 Comment(0)
A
0

filesize takes the name of the file as argument not a URL and it returns the size of the file in bytes. You can divide the return value with 1024 to get the size in KB.

Aam answered 27/9, 2010 at 15:25 Comment(1)
See my comment to Stewie's answer. You can use files size for a remote file, if only your environment and server are correctly configured.Hera
L
0

I had the same problem, which i solved like this. I don't know how optimal it is, but it works for me:

getimagesize("http://localhost/projects/site/schwe/user/1/1.jpg");

$file_size = $file[0]*$file[1]*$file["bits"];
Leech answered 3/10, 2012 at 13:45 Comment(1)
If filesize() does not work with HTTP resources, getimagesize() will also fail. Calculating the filesize by multiplying uncompressed image properties will also lead to wrong results.Collins

© 2022 - 2024 — McMap. All rights reserved.