国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php獲取遠程圖片體積大小的實例

2024-09-04 11:46:56
字體:
來源:轉載
供稿:網友

有時候,我們需要知道遠程圖片的體積大小,那么PHP如何可以做到呢?

直接看代碼吧,很好明白的,代碼如下:

  1. //用法 echo remote_filesize($url,$user='',$pw=''); 
  2. $url = "http://www.aa.com/librarys/images/random/rand_11.jpg";//這里要換成你的圖片地址 
  3. echo remote_filesize($url,$user='',$pw=''); 
  4.  
  5. function remote_filesize($uri,$user='',$pw=''
  6. // start output buffering 
  7.     ob_start(); 
  8. // initialize curl with given uri 
  9.     $ch = curl_init($uri); // make sure we get the header 
  10.     curl_setopt($ch, CURLOPT_HEADER, 1); // make it a http HEAD request 
  11.     curl_setopt($ch, CURLOPT_NOBODY, 1); // if auth is needed, do it here 
  12.     if (!emptyempty($user) && !emptyempty($pw)) 
  13.     { 
  14.         $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw)); 
  15.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
  16.     } 
  17.     $okay = curl_exec($ch); 
  18.     curl_close($ch); // get the output buffer 
  19.     $head = ob_get_contents(); // clean the output buffer and return to previous // buffer settings 
  20.     ob_end_clean();  // gets you the numeric value from the Content-Length // field in the http header 
  21.     $regex = '/Content-Length:/s([0-9].+?)/s/'
  22.     $count = preg_match($regex$head$matches);  // if there was a Content-Length field, its value // will now be in $matches[1] 
  23.     if (isset($matches[1])) 
  24.     { 
  25.         $size = $matches[1]; 
  26.     }  //Vevb.com 
  27.     else 
  28.     { 
  29.         $size = 'unknown'
  30.     } 
  31.     $last_mb = round($size/(1024*1024),3); 
  32.  $last_kb = round($size/1024,3); 
  33.     return $last_kb . 'KB / ' . $last_mb.' MB'

函數的思路是,先CURL獲取圖片到緩沖區,然后正則獲取圖片的Content-Length信息就OK了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚宁县| 临颍县| 修武县| 澄迈县| 开江县| 共和县| 淳安县| 碌曲县| 兴义市| 金华市| 隆尧县| 渝中区| 司法| 禹城市| 静乐县| 田林县| 平罗县| 镶黄旗| 明水县| 江西省| 邢台县| 平山县| 综艺| 霸州市| 墨玉县| 清镇市| 老河口市| 邻水| 交城县| 甘洛县| 莱阳市| 玉龙| 鹤峰县| 清徐县| 滁州市| 遂溪县| 兴山县| 东阿县| 广宁县| 永城市| 明光市|