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



     

  • 相关阅读:
    7.21 高博教育 数组 内存
    【基础扎实】Python操作Excel三模块
    PAT 甲级 1012 The Best Rank
    PAT 甲级 1011  World Cup Betting
    PAT 甲级 1010 Radix
    链式线性表——实验及提升训练
    循环程序设计能力自测
    链表应用能力自测
    PAT 甲级 1009 Product of Polynomials
    1008 Elevator (20分)
  • 原文地址:https://www.cnblogs.com/rxbook/p/11402228.html
Copyright © 2011-2022 走看看