PHP 獲取遠(yuǎn)程文件大小的3種解決方法
更新時間:2013年07月11日 09:39:11 作者:
以下是對PHP中獲取遠(yuǎn)程文件大小的3種解決方法進(jìn)行了詳細(xì)的介紹,需要的朋友參考下
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.1\r\n");
fputs($fp, "Host:" . $url['host']. "\r\n\r\n");
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;
}
}
?>
復(fù)制代碼 代碼如下:
<?php
$file = file_get_contents($url);
echo strlen($file);
?>
2. 使用get_headers()
復(fù)制代碼 代碼如下:
<?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()
復(fù)制代碼 代碼如下:
<?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.1\r\n");
fputs($fp, "Host:" . $url['host']. "\r\n\r\n");
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;
}
}
?>
相關(guān)文章
PHP數(shù)據(jù)庫調(diào)用類調(diào)用實例(詳細(xì)注釋)
PHP開發(fā)中我們經(jīng)常需要用一些數(shù)據(jù)庫類,這里簡單的分享下調(diào)用類的代碼,學(xué)習(xí)php數(shù)據(jù)庫操作的朋友可以參考下2012-07-07通過JavaScript或PHP檢測Android設(shè)備的代碼
在此列出一些能夠在iOS的最大競爭者——安卓(Android)系統(tǒng)的檢測方法。即通過JavaScript或PHP檢測Android設(shè)備,給大家提供參考。2011-03-03spl_autoload_register與autoload的區(qū)別詳解
本篇文章是對spl_autoload_register與autoload的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP 5.3和PHP 5.4出現(xiàn)FastCGI Error解決方法
這篇文章主要介紹了PHP 5.3和PHP 5.4出現(xiàn)FastCGI Error解決方法,需要的朋友可以參考下2015-02-02