求解…获取远程文件大小

<?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.1\r\n");
fputs($fp,"Host:$url[host]\r\n\r\n");
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://wpa.qq.com/pa?p=2:931751994:41&r=1368852898");
?>
无法获取这样文件连接的文件大小…xx.com/logo.gif这样的就能获取!求解?@_@eg:http://hu60.cn/wap/read.php?id=bbs_tz&tzid=43792……解决了。用get_headers()获得Location然后判断地止
回复列表(3|显示机器人聊天)
  • @Ta / 2013-05-19 / /
    header重定向了
  • @Ta / 2013-05-19 / /
    curl好像可以吧?
  • xlm
    @Ta / 2013-05-19 / /
    function getrFSize($url){
    	$ui=parse_url($url);
    	if(!$fh=@get_headers($url))
    		return 0;
    	$fs=0;
    	foreach($fh as $k=>$v){
    		if(preg_match('/Location:(.*)/is',$v,$matches)){
    			$matches[1]=trim($matches[1]);
    			if($matches[1][0]=='/'){
    				$nu="http://{$ui['host']}{$matches[1]}";
    			}elseif(preg_match('~^http://~is',$matches[1])){
    				$nu=$matches[1];
    			}else{
    				$ui['path']=dirname($ui['path']);
    				$nu="http://{$ui['host']}{$ui['path']}/{$matches[1]}";
    			}
    			return getFSize($nu);
    		}elseif(preg_match('/Content-Length:(.*)/is',$v,$matches)){
    			$fs=intval(trim($matches[1]));
    		}
    	}
    	return $fs;
    }
    
添加新回复
回复需要登录