PHP gets an instance of the remote picture volume size _php instance

Source: Internet
Author: User
Just look at the code, it's good to know.
Copy CodeThe code is as follows:
Usage echo remote_filesize ($url, $user = ", $PW =");
$url = "http://www.aa.com/librarys/images/random/rand_11.jpg";//change your picture address here
Echo remote_filesize ($url, $user = ", $PW =");

function Remote_filesize ($uri, $user = ", $PW =")
{
Start output buffering
Ob_start ();
Initialize curl with given URI
$ch = Curl_init ($uri); Make sure we get the header
curl_setopt ($ch, Curlopt_header, 1); Make it a HTTP HEAD request
curl_setopt ($ch, curlopt_nobody, 1); If Auth is needed, does it here
if (!empty ($user) &&!empty ($PW))
{
$headers = Array (' Authorization:basic '. Base64_encode ($user. ': ' $pw));
curl_setopt ($ch, Curlopt_httpheader, $headers);
}
$okay = curl_exec ($ch);
Curl_close ($ch); Get the output buffer
$head = Ob_get_contents (); Clean the output buffer and return to previous//buffer settings
Ob_end_clean (); Gets the numeric value from the Content-length//field in the HTTP header
$regex = '/content-length:\s ([0-9].+?) \s/';
$count = Preg_match ($regex, $head, $matches); If there is a content-length field, its value//would now is in $matches [1]
if (Isset ($matches [1]))
{
$size = $matches [1];
}
Else
{
$size = ' unknown ';
}
$last _MB = Round ($size/(1024*1024), 3);
$last _kb = round ($size/1024,3);
Return $last _kb. ' KB/'. $last _mb. ' MB ';
}

The idea of the function is that curl gets the picture to the buffer, then the content-length information of the image is OK.

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