php how to get web image size in kb?
Solution 1
Short of doing a complete HTTP request, there is no easy way:
$img = get_headers("http://static.adzerk.net/Advertisers/2564.jpg", 1);
print $img["Content-Length"];
You can likely utilize cURL
however to send a lighter HEAD
request instead.
Solution 2
<?php
$file_size = filesize($_SERVER['DOCUMENT_ROOT']."/Advertisers/2564.jpg"); // Get file size in bytes
$file_size = $file_size / 1024; // Get file size in KB
echo $file_size; // Echo file size
?>
Solution 3
Not sure about using filesize()
for remote files, but there are good snippets on php.net though about using cURL.
http://www.php.net/manual/en/function.filesize.php#92462
Solution 4
That sounds like a permissions issue because filesize() should work just fine.
Here is an example:
php > echo filesize("./9832712.jpg");
1433719
Make sure the permissions are set correctly on the image and that the path is also correct. You will need to apply some math to convert from bytes to KB but after doing that you should be in good shape!
Solution 5
Here is a good link regarding filesize()
You cannot use filesize() to retrieve remote file information. It must first be downloaded or determined by another method
Using Curl here is a good method:
Comments
-
fish man almost 2 years
php how to get web image size in kb?
getimagesize
only get the width and height.and
filesize
causedwaring
.$imgsize=filesize("http://static.adzerk.net/Advertisers/2564.jpg"); echo $imgsize;
Warning: filesize() [function.filesize]: stat failed for http://static.adzerk.net/Advertisers/2564.jpg
Is there any other way to get a web image size in kb?
-
fish man almost 13 yearsgreat, get_headers run faster. Thanks.
-
Darien almost 13 yearsMake sure your HTTP client is not sending any headers saying it accepts gzipped HTTP responses, or else the
Content-Length
will be wrong because the server will send compressed data back. -
mario almost 13 years@Darien: Excellent catch! Luckily
get_headers
sends a pretty plain HTTP/1.0 request. But for cURL this would need more diligence. -
Gordon over 12 years
get_headers
can also do a HEAD request. there is an example in the php manual showing how. -
maxisme almost 7 yearsThis sometimes returns an array:
Array ( [0] => 0 [1] => 103823 )
How do I handle that?get_headers($imageURL, 1)["Content-Length"]
where$imageURL
is s-media-cache-ak0.pinimg.com/originals/23/bb/f3/…