- 相關推薦
PHP獲取遠程文件大小的3種解決方法
導語:PHP獲取遠程文件大小的3種解決方法,你懂了那些?以下是對PHP中獲取遠程文件大小的3種解決方法進行了詳細的介紹,需要的朋友參考下
1、使用file_get_contents()
復制代碼 代碼如下:
<?php
$file = file_get_contents($url);
echo strlen($file);
?>
2. 使用get_headers()
復制代碼 代碼如下:
<?php
$header_array = get_headers($url, true);
$size = $header_array['Content-Length'];
echo $size;
?>
PS:
需要打開allow_url_fopen!
如未打開會顯示
Warning: get_headers() [function.get-headers]: URL file-access is disabled in the server configuration
3.使用fsockopen()
復制代碼 代碼如下:
<?php
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;
}
}
?>
【PHP獲取遠程文件大小的3種解決方法】相關文章:
PHP獲取遠程文件大小的解決方法04-06
PHP獲取星期的方法07-06
PHP如何獲取表單07-27
修改PHP上傳文件大小限制05-23
php怎么獲取input的id04-12
php創建cookie獲取方法02-10
PHP如何獲取系統信息02-02
使用PHP獲取數組的鍵與值03-20
PHP實現獲取域名的方法小結02-03