php 远程视频文件大小,PHP获取远程文件大小的多种方法

PHPer都知道,本地获取文件大小,很简单filesize($path) ,OK了,

那很获取远程文件大小呢?

在网上看到不少方法,整合下,推荐给大家。

利用get_headers(),其实这是一个值得关注的函数,但是关注的人却不是很多。

它可以取得服务器响应一个 HTTP 请求所发送的所有标头

方法(1):

get_headers($url,true);//true|1表示 它会解析相应的信息并设定数组的键名

返回结果如下:

http://192.168.2.189/xiaojhmanage//Public/Uploads/2014-11-24/5472d67b425c9.apk

 
 

Array

(

[0] => HTTP/1.1 200 OK

[Server] => nginx/1.0.15

[Date] => Fri, 05 Dec 2014 03:53:10 GMT

[Content-Type] => application/octet-stream

[Content-Length] => 6845178

[Last-Modified] => Mon, 24 Nov 2014 06:55:55 GMT

[Connection] => close

[Accept-Ranges] => bytes

)

//Content-Length即表示大小[单位字节]

方法(2),

网上找的一个函数

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;              if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)) return trim($arr[1]);         }         return null;     } else {         return null;     }

}

主要通过 fsockopen 来实现

方法(3),通过CURL来获取

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();

//     echo '
head-->'.$head.'';     $regex = '/Content-Length:\s([0-9].+?)\s/';     $count = preg_match($regex, $head, $matches);     return isset($matches[1]) ? $matches[1] : 'unknown';

}

方法(4),file_get_contents(),虽然它的功能不强大,但也是一种方法。

echo strlen(file_get_contents($url));  //抓取大文件比较吃力,建议不要使用其它

试试吧,各位!

最后编辑:2014-12-05作者:sunny5156

0241ce914f3ca1e001b07a6287273dc5?s=96&d=retro&r=g

喜欢技术....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值