Use cURL to obtain remote filesize

Sometimes you might want to determine a remote file size prior to determining further action to take. This is a quick function that utilises cURL to retrieve the file size in bytes.
<?php
function remoteFilesize ($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
curl_close($ch);
$info = explode('Content-Length:', $info);
$info = explode("Connection",$info[1]);
return $info[0];
}
?>

Usage is simple:

echo remoteFilesize("test.com/file.html");

Leave a Reply

Your email address will not be published. Required fields are marked *