Home » Php » filesize – php get the KB size of an image

filesize – php get the KB size of an image

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

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' );

Answer:

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.

Answer:

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);                      
    }
}

Answer:

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.

Answer:

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"];