PHP獲取遠端檔案大小 如果加判斷檔案大小以KB MB GB
阿新 • • 發佈:2019-01-28
<?php function sizecount($filesize) { if($filesize >= 1073741824) { $filesize = round($filesize / 1073741824 * 100) / 100 . ' gb'; } elseif($filesize >= 1048576) { $filesize = round($filesize / 1048576 * 100) / 100 . ' mb'; } elseif($filesize >= 1024) { $filesize = round($filesize / 1024 * 100) / 100 . ' kb'; } return $filesize; } function getFileSize($url){ $url = parse_url($url); if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)){ fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n"); fputs($fp,"Host:$url[host]\r\n\r\n"); while(!feof($fp)){ $tmp = fgets($fp); if(trim($tmp) == ''){ break; }else if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)){ return sizecount(trim($arr[1])); } } return null; }else{ return null; } } //呼叫方法 echo getFileSize('http://bbs.phpchina.com/template/phpchina/image/common/logo.gif'); ?>