Home >Backend Development >PHP Tutorial >3 solutions to get remote file size in PHP_PHP Tutorial

3 solutions to get remote file size in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:47870browse

1. Use file_get_contents()

Copy the code The code is as follows:

$file = file_get_contents($url);
echo strlen($file);
?>

2. Use get_headers()
Copy code The code is as follows:

$header_array = get_headers($url, true);
$size = $header_array['Content-Length'];
echo $size;
?>

PS:
required Open allow_url_fopen!
If it is not opened, it will be displayed
Warning: get_headers() [function.get-headers]: URL file-access is disabled in the server configuration
3. Use fsockopen ()
Copy code The code is as follows:

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) {
                                                                                                                          host']. "rnrn");

while (!feof($fp)) {
$str = fgets($fp);
if (trim($str) == ' ') {
break;
}elseif(preg_match('/Content-Length:(.*)/si', $str, $arr)) {
return trim($arr[1]) ;
                                                                               ; 🎜> }
?>





http://www.bkjia.com/PHPjc/328048.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/328048.html

1. Use file_get_contents() to copy the code. The code is as follows: ?php $file = file_get_contents($url); echo strlen ($file); ? 2. Use get_headers() to copy the code. The code is as follows: ?php $header_...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn