php获取远程文件大小的四种方法

时间:2016-08-19
我们都知道,获取本地文件大小可以使用php filesize()函数,但如何获取远程文件的大小呢?本文章向大家介绍php获取远程文件大小的四种方法,分别get_headers()方法、fsockopen()方法、curl_init方法和file_get_contents方法,需要的朋友可以参考一下。

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 ' . 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 '<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;