php获取远程文件大小的四种方法
get_headers()函数获取远程文件大小get_headers() 是PHP系统级函数,他返回一个包含有服务器响应一个 HTTP 请求所发送的标头的数组。该数组中有一个值表示远程文件的大小,即Content-Length。 $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.1rn"); fputs($fp,"Host:$url[host]rnrn"); 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 ' . base64_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 ' // 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; (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
