Get the filesize of remote file

sandykadam

202.***.***.***
1,172 days ago

Get the filesize of remote file

If you want to find the file size of remote files which are on different sites you can find by following code.

PHP code:


<?php

function getRemoteFileSize($url){
$parsed = parse_url($url);
$host = $parsed["host"];
$fp = @fsockopen($host, 80, $errno, $errstr, 20);
if(!$fp)return false;
else {
@fputs($fp, "HEAD $url HTTP/1.1\r\n");
@fputs($fp, "HOST: $host\r\n");
@fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while(!@feof($fp))$headers .= @fgets ($fp, 128);
}
@fclose ($fp);
$return = false;
$arr_headers = explode("\n", $headers);
foreach($arr_headers as $header) {
$s = "Content-Length: ";
if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
$return = trim(substr($header, strlen($s)));
break;
}
}
if($return){
$size = round($return / 1024, 2);
$sz = "KB"; // Size In KB
if ($size > 1024) {
$size = round($size / 1024, 2);
$sz = "MB"; // Size in MB
}
$return = "$size $sz";
}
return $return;
}

//File size of Google Image
echo "yahoo Logo Size : <b>" . getRemoteFileSize('http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif') . "</b>  (http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif)";
echo "<br>google logo sized Image : <b>" . getRemoteFileSize('http://www.google.co.in/intl/en_com/images/logo_plain.png') ."</b>  (http://www.google.co.in/intl/en_com/images/logo_plain.png)";

?>



Output will be:

yahoo Logo Size : 1.83 KB (http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif)
google logo sized Image : 7.88 KB (http://www.google.co.in/intl/en_com/images/logo_plain.png)