php获得远程文件大小的函数

php技巧

 文件的大小函数为:filesize()
文件是否存在的函数为:file_exits();
但是这两个函数只针对本地

那么:远程文件是否存在,远程文件大小 如何得知呢?

搜索了一下,有人居然说,把远程文件下载过来再判断这个远程文件的大小,这是什么歪理。

庆幸大部分人还是清醒的,一般应该使用判断header反馈的信息进行判断。

php中如何获得header信息呢? php的函数真多,这个也不例外

1.最简单的获取远程文件大小办法

1
$a_array = get_headers(url,true);

url就是网址了,至于第二个参数

就可以得到类似下面的这个数组

1
2
3
4
5
6
7
8
9
10
11
12
Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Sat, 29 May 2004 12:28:14 GMT
    [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
    [ETag] => "3f80f-1b6-3e1cb03b"
    [Accept-Ranges] => bytes
    [Content-Length] => 438
    [Connection] => close
    [Content-Type] => text/html
)

所以,你可以很舒服的拿到远程文件的大小

1
$file_sizeofurl = a_array['Content-Length'];

2.用curl获取远程文件大小
如果服务器禁止get_headers 怎么办?
换一种办法,用curl
我总觉得curl就像一个虚拟的用户,什么都能模仿

下面直接给出一个老外的函数
请注意

1
echo '<br>head-->'.$head.'<----end <br>';

这句是我加的,为了知道header里面到底包含了什么东西

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 (!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); 
// 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;
}

3.fsock获取远程文件大小的办法
先给函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function getFileSize($url){ 
        $url = parse_url($url);
        if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)){
                fputs($fp,"GET ".(empty($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;
                        }else if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)){
                                return trim($arr[1]);
                        }
                }
                return null;
        }else{
                return null;
        }
}

哪个获取远程文件大小最快?
针对同一个url进行测试,curl > fsock > getheader
针对不同url测试,结果还是 curl > fsock > getheader
当然也许这个测试是不准确的,但getheader函数是明显要慢一些的

考虑到curl模块没有fsock那么普及,所以我自己还是用后面一个

速度上的差别大约是 curl比fsock快0.2秒,fsock比getheader快0.2秒。

远程文件的大小拿来干嘛用?
好像某些人用来分块下载文件
我是拿来判断远程文件是否更新(虽然不准),其他人有什么好办法不?给我留言吧

One Response to “php获得远程文件大小的函数”

  1. Qiyo Says:
    September 14th, 2008 at 6:13 pm

    谢谢了… 找了半天才找到…. 非常感谢…

Leave a Reply

Icons by N.Design Studio. Designed By Ben Swift. Powered by WordPress and Free WordPress Themes
Entries RSS Comments RSS Log in