源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP 获取远程文件大小的3种解决方法

  • 时间:2020-07-06 07:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP 获取远程文件大小的3种解决方法
[b]1、使用file_get_contents() [/b]
[u]复制代码[/u] 代码如下:
<?php $file = file_get_contents($url); echo strlen($file); ?>
[b]2. 使用get_headers() [/b]
[u]复制代码[/u] 代码如下:
<?php $header_array = get_headers($url, true); $size = $header_array['Content-Length']; echo $size; ?>
[b]PS: 需要打开allow_url_fopen! [/b]如未打开会显示 Warning: get_headers() [function.get-headers]: URL file-access is disabled in the server configuration [b]3.使用fsockopen() [/b]
[u]复制代码[/u] 代码如下:
<?php  function get_file_size($url) {      $url = parse_url($url);      if (empty($url['host'])) {          return false;      }      $url['port'] = empty($url['post']) ? 80 : $url['post'];      $url['path'] = empty($url['path']) ? '/' : $url['path'];      $fp = fsockopen($url['host'], $url['port'], $error);      if($fp) {          fputs($fp, "GET " . $url['path'] . " HTTP/1.1rn");          fputs($fp, "Host:" . $url['host']. "rnrn");          while (!feof($fp)) {              $str = fgets($fp);              if (trim($str) == '') {                  break;              }elseif(preg_match('/Content-Length:(.*)/si', $str, $arr)) {                  return trim($arr[1]);              }          }          fclose ( $fp);          return false;      }else {          return false;      }  }  ?>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部