天下風雲出我輩,一入江湖歲月催,皇圖霸業談笑中,不勝人生一場醉。以下是小編爲大家搜索整理的PHP獲取遠程文件大小的解決方法,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!
1、使用file_get_contents()
複製代碼 代碼如下:
$file = file_get_contents($url);
echo strlen($file);
?>
2. 使用get_headers()
複製代碼 代碼如下:
$header_array = get_headers($url, true);
$size = $header_array['Content-Length'];
echo $size;
?>
PS:
需要打開allow_url_fopen!
如未打開會顯示
Warning: get_headers() [-headers]: URL file-access is disabled in the server configuration
3.使用fsockopen()
複製代碼 代碼如下:
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;
}
}
?>