PHP 3 ways to get the size of a remote file _php tips

Source: Internet
Author: User
1. Use file_get_contents ()
Copy Code code as follows:

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

2. Use of Get_headers ()
Copy Code code as follows:

<?php
$header _array = Get_headers ($url, true);
$size = $header _array[' content-length '];
Echo $size;
?>

PS:
Need to open allow_url_fopen!
If not open, it will display
Warning:get_headers () [function.get-headers]: URL file-access is disabled in the server configuration
3. Use of Fsockopen ()
Copy Code code as follows:

<?php
function Get_file_size ($url) {
$url = Parse_url ($url);

if (Empty ($url [' Host '])) {
return false;
}

$url [' port '] = Empty ($url [' post '])? : $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;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.