【PHP】获取远程文件的字节大小

 1 <?php
 2 
 3 /**
 4      * @description 获取远程文件的字节大小
 5      * @param $url 远程文件地址
 6      * @return int|mixed 
 7      */
 8     function getFileSize($url){
 9         ob_start();
10         $ch = curl_init($url);
11         curl_setopt($ch, CURLOPT_HEADER, 1);
12         curl_setopt($ch, CURLOPT_NOBODY, 1);
13         curl_setopt($ch, CURLOPT_HTTPGET,1);
14         curl_exec($ch);
15         curl_close($ch);
16         $head = ob_get_contents();
17         ob_end_clean();
18         $regex = '/Content-Length:\s([0-9].+?)\s/';
19         preg_match($regex, $head, $matches);
20         return isset($matches[1]) && is_numeric($matches[1]) ? $matches[1] : 0;
21     }
22 
23 ?>

 

posted @ 2021-09-15 14:29  bug毁灭者  阅读(138)  评论(0编辑  收藏  举报