PHP 3 ways to get the remote file size _php Tutorial

Source: Internet
Author: User
1. Using file_get_contents ()
Copy CodeThe code is as follows:
$file = file_get_contents ($url);
echo strlen ($file);
?>

2. Using Get_headers ()
Copy CodeThe code is as follows:
$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 show
Warning:get_headers () [function.get-headers]: URL file-access is disabled in the server configuration
3. Using Fsockopen ()
Copy CodeThe code is as follows:
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;
}
}
?>

http://www.bkjia.com/PHPjc/328048.html www.bkjia.com true http://www.bkjia.com/PHPjc/328048.html techarticle 1. Use file_get_contents () to copy code as follows: PHP $file = file_get_contents ($url); Echo strlen ($file); 2. Use Get_headers () to copy the code as follows:? php $header _ ...

  • 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.