<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. cURL is a command-line tool and a library, that allows you to transfer data to or from a server using various protocols, such as HTTP, FTP, and others. One useful feature of cURL is the ability to get the file size of a remote file without downloading it. To do this, you can use the -I option followed by the URL of the file you want to check. This option sends a HEAD request to the server, which only retrieves the header of the file and not the actual content. The server responds with the file's metadata, including the file size. In the response, look for the "Content-Length" header, which specifies the size of the file in bytes. Note that the file size may be rounded up or down by the server, depending on the server's configuration.



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