php获取文件大小的4种方法

jerry PHP 2015年11月18日 收藏
在网上找了4种获取文件大小的方法 记录起来。。。。
方法一:header
  1. <?php   
  2. get_headers($url,true);   
  3.    
  4. //返回结果    
  5. Array   
  6. (   
  7.     [0] => HTTP/1.1 200 OK   
  8.     [Date] => Sat, 29 May 2004 12:28:14 GMT   
  9.     [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)   
  10.     [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT   
  11.     [ETag] => "3f80f-1b6-3e1cb03b"   
  12.     [Accept-Ranges] => bytes   
  13.     <STRONG>[Content-Length] => 438 </STRONG>  
  14.     [Connection] => close   
  15.     [Content-Type] => text/html   
  16. )   
  17. ?>   


方法二
  1. function getFileSize($url)   
  2. {   
  3.     $url = parse_url($url);   
  4.     if($fp = @fsockopen($url['host'],emptyempty($url['port'])?80:$url['port'],$error))   
  5.     {   
  6.         fputs($fp,"GET ".(emptyempty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");   
  7.         fputs($fp,"Host:$url[host]\r\n\r\n");   
  8.         while(!feof($fp))   
  9.         {   
  10.             $tmp = fgets($fp);   
  11.             if(trim($tmp) == '')   
  12.             {   
  13.                 break;   
  14.             }   
  15.             elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))   
  16.             {   
  17.                 return trim($arr[1]);   
  18.             }   
  19.         }   
  20.         return null;   
  21.     }   
  22.     else   
  23.     {   
  24.         return null;   
  25.     }   
  26. }  
方法三:
  1. function remote_filesize($uri,$user='',$pw='')   
  2. {   
  3.     // start output buffering    
  4.     ob_start();   
  5.     // initialize curl with given uri    
  6.     $ch = curl_init($uri);   
  7.     // make sure we get the header    
  8.     curl_setopt($ch, CURLOPT_HEADER, 1);   
  9.     // make it a http HEAD request    
  10.     curl_setopt($ch, CURLOPT_NOBODY, 1);   
  11.     // if auth is needed, do it here    
  12.     if (!emptyempty($user) && !emptyempty($pw))   
  13.     {   
  14.         $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));   
  15.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);   
  16.     }   
  17.     $okay = curl_exec($ch);   
  18.     curl_close($ch);   
  19.     // get the output buffer    
  20.     $head = ob_get_contents();   
  21.     // clean the output buffer and return to previous    
  22.     // buffer settings    
  23.     ob_end_clean();   
  24.    
  25.     echo '<br>head-->'.$head.'<----end <br>';   
  26.    
  27.     // gets you the numeric value from the Content-Length    
  28.     // field in the http header    
  29.     $regex = '/Content-Length:\s([0-9].+?)\s/';   
  30.     $count = preg_match($regex, $head, $matches);   
  31.    
  32.     // if there was a Content-Length field, its value    
  33.     // will now be in $matches[1]    
  34.     if (isset($matches[1]))   
  35.     {   
  36.         $size = $matches[1];   
  37.     }   
  38.     else   
  39.     {   
  40.         $size = 'unknown';   
  41.     }   
  42.     //$last=round($size/(1024*1024),3);    
  43.     //return $last.' MB';    
  44.     return $size;   
  45. }   
方法四:
  1. $fCont = file_get_contents("http://www.weiyeying.cn/");   
  2. echo strlen($fCont)/1024;