2

I am trying to get image size using

get_headers($url)

But I am getting warning like

get_headers() [function.get-headers]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in

Is there any other way to Do so?

Thanks.

Manish Chauhan
  • 595
  • 2
  • 7
  • 14

2 Answers2

18

You can use curl_getinfo() function to get the remote file size.

 $ch = curl_init('http://www.google.co.in/images/srpr/logo4w.png');

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_HEADER, TRUE);
 curl_setopt($ch, CURLOPT_NOBODY, TRUE);

 $data = curl_exec($ch);
 $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

 curl_close($ch);
 echo $size;
Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42
9

You must perform a HEAD request. This will tell the server that you're only interested in the HTTP headers.

stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);
$headers = get_headers('http://example.com');

This will return an array of headers. You must find the content-length item which has the file size (number of bytes).

Webberig
  • 2,746
  • 1
  • 23
  • 19