PHP获取远程文件的大小,通过ob_get_contents实现

function remote_filesize($uri,$user='',$pw='')
{
 ob_start();
 $ch = curl_init($uri);
 curl_setopt($ch, CURLOPT_HEADER, 1);
 curl_setopt($ch, CURLOPT_NOBODY, 1);
 if (!empty($user) && !empty($pw)) {
  $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 }
 $okay = curl_exec($ch);
 curl_close($ch);
 $head = ob_get_contents();
 ob_end_clean();
 $regex = '/Content-Length:\s([0-9].+?)\s/';
 $count = preg_match($regex, $head, $matches);
 //qq($head);
 return isset($matches[1]) ? $matches[1] : 'unknown';
}


注意: $head = ob_get_contents(); 得到的是文件的头部信息:
HTTP/1.1 200 OK
Server: Tengine
Content-Type: image/jpeg
Content-Length: 58034
Connection: keep-alive
Date: Tue, 15 Dec 2015 08:44:10 GMT
Last-Modified: Tue, 15 Dec 2015 08:40:48 GMT
Expires: Thu, 14 Jan 2016 08:44:10 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes
Via: cache37.l2cm9-1[0,200-0,H], cache11.l2cm9-1[0,0], kunlun7.cn119[0,200-0,H], kunlun6.cn119[0,0]
Age: 1105601
X-Cache: HIT TCP_MEM_HIT dirn:9:850558688
X-Swift-SaveTime: Thu, 24 Dec 2015 04:29:36 GMT
X-Swift-CacheTime: 1829674
Timing-Allow-Origin: *
EagleId: 1bdd224614512746510381352e



PHP获取远程文件的大小,通过ob_get_contents实现

 

 
PHP获取远程文件的大小,通过ob_get_contents实现

上一篇:js 二级联动


下一篇:html转word