I would like to check the filesize in an application before I download it.
So, I found this code http://snipplr.com/view/29/get-remote-filesize/ and I changed it a little bit in order to make it more readable.
function remotefilesize($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 200
$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); //filesize
curl_close($ch);
if( 200 != $status )
throw new Exception('File not found!');
return $filesize;
}
printf('PHP logo has %s bytes', remotefilesize("http://br2.php.net/images/php.gif") );