zoukankan      html  css  js  c++  java
  • PHP获取远程文件的大小,通过ob_get_contents实现

    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



     

  • 相关阅读:
    CMDB 理论
    分布式
    闲着无聊 一个python的,三级菜单。装逼版。
    献上一段,派遣网易云音乐,音频的代码。
    redis 安装
    selenium之 chromedriver与chrome版本映射表(更新至v2.46)
    简单的爬虫
    anaconda使用方法
    crm开发之用户重置密码
    模块和包,logging模块
  • 原文地址:https://www.cnblogs.com/rxbook/p/11402228.html
Copyright © 2011-2022 走看看