Problem returning the size of a file with PHP and Curl

Asked

Viewed 114 times

0

I am trying to return the size of a file with php and curl from an external host but when running Curl download_content_length comes as -1, could someone help me?

2 answers

2

I made a simple example below using jQuery’s JS.

 $url = 'http://code.jquery.com/jquery-1.11.1.min.js';
 $ch = curl_init( $url );

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

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

 curl_close($ch);

 echo ($size / 1024) . ' kb';

output: 93.541015625 kb

-1

When using CURL there are many things that can go wrong, so I recommend that you use some component to perform these requests I use the Guzzle so you’ll have friendlier mistakes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.