Php uses the request header to obtain the remote Image size

Source: Internet
Author: User
Php uses the request header to obtain the remote Image size

  1. $ Fp = fsockopen ("www.baidu.com", 80, $ errno, $ errstr, 30 );
  2. If ($ fp ){
  3. // Set the request to HEAD.
  4. $ Out = "HEAD/img/baidu_sylogo1.gif HTTP/1.1 \ r \ n ";
  5. $ Out. = "Host: www.baidu.com \ r \ n ";
  6. $ Out. = "Connection: Close \ r \ n ";
  7. Fwrite ($ fp, $ out );
  8. While (! Feof ($ fp )){
  9. $ Header = fgets ($ fp );
  10. If (stripos ($ header, 'Content-length ')! = False ){
  11. $ Size = trim (substr ($ header, strpos ($ header, ':') + 1 ));
  12. Echo $ size;
  13. }
  14. }
  15. Fclose ($ fp );
  16. } Else {
  17. Echo "$ errstr ($ errno )";
  18. }

Like initiating a GET request, you only need to set the request type "GET" to "HEAD. Modify the requested host and path as needed.

Summary: php can also use get_headers to obtain header information. this function has been tested and is a GET request. for details, refer to: the php function get_headers is a HEAD request or GET request. In addition, some servers may block HEAD Requests. if they are blocked, they can only use GET requests.

In this way, you can use the ready-made function getimagesize to obtain the image size.

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.