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

这段代码展示了如何使用PHP的cURL库获取远程文件的大小。通过设置cURL选项,获取HTTP响应头中的`Content-Length`字段来确定文件大小。如果存在认证信息,它也会被正确地添加到请求头中。
摘要由CSDN通过智能技术生成

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

1d158386bfef22652e1aa358a4c59e0d.png

C802A33A7DD44B64ABFD421D41AAF06E

标签:head,ch,get,ob,2015,curl,PHP,GMT

来源: https://www.cnblogs.com/rxbook/p/11402228.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值