May 7, 2014

Retrieve remote file size

Retrieve remote file size

function retrieve_remote_file_size($url){
$ch = curl_init($url);

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

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

curl_close($ch);
return formatSizeUnits($size);
}
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ‘ GB’;
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ‘ MB’;
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ‘ KB’;
}
elseif ($bytes > 1)
{
$bytes = $bytes . ‘ bytes’;
}
elseif ($bytes == 1)
{
$bytes = $bytes . ‘ byte’;
}
else
{
$bytes = ‘0 bytes’;
}

return $bytes;
}

Last updated: May 7, 2014