<tutorialjinni.com/>

cURL Get File Size Without Download

Posted Under: cURL, PHP, Shell, Tutorials on Feb 20, 2022
cURL Get File Size Without Download
This code snippet will get the size of the remote file without actually downloading it. It is important to remember that file size is present in the content-length field of the HTTP response header. It is an optional header and also cannot be trusted. Use it with caution. We can also use HTTP HEAD method but many security conscious server admins only allow GET and POST requests.

PHP Get File Size URL

$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.tutorialjinni.com/favicon.ico");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);

curl_setopt($ch, CURLOPT_HEADER,1);//HEADER REQURIED
curl_setopt($ch, CURLOPT_NOBODY,1); // NO CONTENT BODY, DO NOT DOWNLOAD ACTUAL FILE
curl_exec($ch);
$CONTENT_LENGTH= curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD); // PARSES THE RESPONSE HEADER AND GET FILE SIZE IN BYTES and -1 ON ERROR.
curl_close($ch);
echo $CONTENT_LENGTH;

cURL Get File Size From Shell

cURL on Windows
curl -sI https://www.tutorialjinni.com/favicon.ico | findstr "Content-Length:"
cURL on Linux
curl -sI https://www.tutorialjinni.com/favicon.ico | grep -i content-length


imgae