2025年3月24日 星期一 甲辰(龙)年 月廿三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > PHP

php 获取远程文件大小

时间:02-20来源:作者:点击数:41

以下实例我们使用php curl 来获取远程文件大小,返回文件大小单位为字节:

  • <?php
  • // 获取远程文件大小函数
  • function remote_filesize($url, $user = "", $pw = "")
  • {
  • ob_start();
  • $ch = curl_init($url);
  • 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);
  • }
  • $ok = 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);
  • return isset($matches[1]) ? $matches[1] . " 字节" : "unknown";
  • }
  • // 实例测试
  • echo remote_filesize("https://www.cdsy.xyz/d/file/computer/programme/html_div_css/2022-02-13/2e41a242eba7796dbc88ca6fff66d0ae.png");
  • ?>

以上实例输出结果为:

  • 8416 字节
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门