欢迎来到福编程网,本站提供各种互联网专业知识!

获取远程文件大小的php函数

发布时间:2010-01-11 作者: 来源:转载
用php实现获取远程文件大小的代码,需要的朋友可以参考下。
复制代码 代码如下:
function getFileSize($url){
$url = parse_url($url);
if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)){
fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1rn");
fputs($fp,"Host:$url[host]rnrn");
while(!feof($fp)){
$tmp = fgets($fp);
if(trim($tmp) == ''){
break;
}else if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)){
return trim($arr[1]);
}
}
return null;
}else{
return null;
}
}
//调用方法
echo getFileSize("http://www.jb51.net/images/logo.gif")
?>

大家运行后,大小应该是4445字节。

相关推荐