get_headers()函数获取远程文件大小
get_headers() 是PHP系统级函数,他返回一个包含有服务器响应一个 HTTP 请求所发送的标头的数组。该数组中有一个值表示远程文件的大小,即Content-Length。
<?php
$url=\'http://www.manongjc.com\';
print_r(get_headers($url));
?>
输出结果:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: \"3f80f-1b6-3e1cb03b\"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
fsockopen()方法获取远程文件大小
function getFileSize($url)
{
$url = parse_url($url);
if($fp = @fsockopen($url[\'host\'],emptyempty($url[\'port\'])?80:$url[\'port\'],$error))
{
fputs($fp,\"GET \".(emptyempty($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;
}
/* http://www.manongjc.com/article/1378.html */
elseif(preg_match(\'/Content-Length:(.*)/si\',$tmp,$arr))
{
return trim($arr[1]);
}
}
return null;
}
else
{
return null;
}
}
curl获取远程文件大小
curl也还比较方便,还能支持用户验证
function remote_filesize($uri,$user=\'\',$pw=\'\')
{
// start output buffering
ob_start();
// initialize curl with given uri
$ch = curl_init($uri);
// make sure we get the header
curl_setopt($ch, CURLOPT_HEADER, 1);
// make it a http HEAD request
curl_setopt($ch, CURLOPT_NOBODY, 1);
// if auth is needed, do it here
if (!emptyempty($user) && !emptyempty($pw))
{
$headers = array(\'Authorization: Basic \' . 64_encode($user.\':\'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$okay = curl_exec($ch);
curl_close($ch);
// get the output buffer
$head = ob_get_contents();
// clean the output buffer and return to previous
// buffer settings
ob_end_clean();
echo \'<br>head-->\'.$head.\'<----end <br>\';
// gets you the numeric value from the Content-Length
// field in the http header
$regex = \'/Content-Length:\\s([0-9].+?)\\s/\';
$count = preg_match($regex, $head, $matches);
// if there was a Content-Length field, its value
// will now be in $matches[1]
if (isset($matches[1]))
{
$size = $matches[1];
}
else
{
$size = \'unknown\';
}
//$last=round($size/(1024*1024),3);
//return $last.\' MB\';
return $size;
}
file_get_contents获取远程文件大小
$fCont = file_get_contents(\"http://www.weiyeying.cn/\");
echo strlen($fCont)/1024; 继续阅读与本文标签相同的文章
上一篇 :
奶爸为8岁女儿解读深度学习篇之:11个事实
-
相继表态,意、英、德欧洲三国皆希望与华为合作,谷歌也不例外
2026-05-14栏目: 教程
-
中国第4大运营商来袭,携号转网套路多,移不动联不通信不过拜拜
2026-05-14栏目: 教程
-
美国让华为更强大,9个月入账6千亿增速超2成,5G合同已签60多份
2026-05-14栏目: 教程
-
未来几年,这4个大学专业最吃香,前景广阔堪称铁饭碗!
2026-05-14栏目: 教程
-
这间屋子没有电话
2026-05-14栏目: 教程
