本节内容:
PHP获取远程文件大小
方法1、使用file_get_contents()
方法2. 使用get_headers()
说明:
需要打开allow_url_fopen!
如未打开会显示
Warning: get_headers() [function.get-headers]: URL file-access is disabled in the server configuration
方法3.使用fsockopen()
<?php
/**
* fsockopen获取远程文件大小
* by www.jb200.com
*/
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;
}
}
?>