I have the following code...
<?
$kb = 1024;
$mb = 1024 * $kb;
$gb = 1024 * $mb;
$tb = 1024 * $gb;
if (!isset($f)) {
$fileurl = "http://domain.com/files/file.mpg";
} else {
$fileurl = $f;
}
$pu = @parse_url($fileurl);
$filename = basename($fileurl);
$domain = $pu['host'];
$file = $pu['path'];
$host = $pu['host'];
$http = "GET ".$file." HTTP/1.0\r\nHost: ".$host."\r\n\r\n";
$fp = fsockopen ($domain, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
}
else {
fputs ($fp, $http);
while (!feof($fp)){
$chunk= fgets ($fp,128);
if (strpos($chunk, "ength:"))
{
$size = explode (':', $chunk);
if($size[1] < $kb) {
$s = $size[1]." B";
}
else if($size[1] < $mb) {
$s = round($size[1]/$kb,2)." KB";
}
else if($size[1] < $gb) {
$s = round($size[1]/$mb,2)." MB";
}
else if($size[1] < $tb) {
$s = round($size[1]/$gb,2)." GB";
}
else {
$s = round($size[1]/$tb,2)." TB";
}
echo "The file $filename has a size of $s";
break;
}
}
fclose ($fp);
}
?>
It works well, but sometimes (mainly in case ftp servers) doesn't get the filesize just a blank page.
Does anybody know how i can fix it?
I'd like if it works both http and ftp protocols.
Thanks
Dedix