5

I Was Using This Code to Get Image size in php and it was working Perfectly For Me.

$img = get_headers("http://ultoo.com/img_single.php", 1);
$size = $img["Content-Length"];
echo $size;

But How Get this through CURL ? I Tried This But Doesn't Work.

$url = 'http://ultoo.com/img_single.php';
$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_HEADER, 1);
 curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Length" );
 //curl_setopt($ch, CURLOPT_NOBODY, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($ch);

 $filesize = $result["Content-Length"];

  curl_close($ch);

  echo $filesize;
user2424807
  • 121
  • 1
  • 2
  • 8

3 Answers3

1

set curl_setopt($ch, CURLOPT_HTTPHEADER, true );,then print_r($result),you will see something like

HTTP/1.1 200 OK
Date: Tue, 04 Jun 2013 03:12:38 GMT
Server: Apache/2.2.15 (Red Hat)
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=rtd17m2uig3liu63ftlobcf195; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 211
Content-Type: image/png

I don't think Content-Length is the right way to get image size,because I get different result between curl and get_header

5z- -
  • 161
  • 4
0

I've run into this before, script I used can be found here -> http://boolean.co.nz/blog/curl-remote-filesize/638/ . Fairly similar to the post above, but a little more straightforward, and much less forgiving.

Refuting logic
  • 341
  • 2
  • 7
  • Parse error: syntax error, unexpected '?', expecting ')' in /home/dotmamat/public_html/d/dd/t1.php on line 10 – user2424807 Jun 04 '13 at 02:20
  • if you did a direct copy paste make sure all the characters are correct, such as double quotes being 'real' double quotes. other than that, not sure where an "unexpected ?" would come from. That function has all its bracket's matched. – Refuting logic Jun 04 '13 at 04:27
0

Maybe this works for you (assuming that page always just returns an img/png) - I included a "write file" just to be able to compare the screen output vs file size (the png from the page):

$url = 'http://ultoo.com/img_single.php'; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return the output as a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //time out length
$data = curl_exec($ch);
if (!$data) {
    echo "<br />cURL error:<br/>\n";
    echo "#" . curl_errno($ch) . "<br/>\n";
    echo curl_error($ch) . "<br/>\n";
    echo "Detailed information:";
    var_dump(curl_getinfo($ch));
    die();
}
curl_close($ch);
$handle = fopen("image.png", "w");
fwrite($handle, $data);
fclose($handle);
$fileSize = strlen($data);
echo "fileSize = $fileSize\n";
nvoigt
  • 75,013
  • 26
  • 93
  • 142
4pi
  • 11
  • 1
  • 2