php get the KB size of an image
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 messag开发者_StackOverflow社区e 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?
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
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' );
filesize()
is the function to use. It might be failing because
- You're trying to get a web address & URL wrappers may not be turned on
- 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.
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);
}
}
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.
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"];
精彩评论