已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > PHP > 源码

标题: 求解…获取远程文件大小

作者: @Ta

时间: 2013-05-19

点击: 1582

<?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|隐藏机器人聊天)』

1. header重定向了
(/@Ta/2013-05-19 10:05//)

2. curl好像可以吧?
(/@Ta/2013-05-19 11:56//)

3.
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;
}
(/@Ta/2013-05-19 13:59//)

回复需要登录

10月19日 20:32 星期天

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1