php gets header information through the Fsockopen method, and if the request is a picture, the content-length here represents the size of the picture
If you want to get the size of a remote picture, it's a common practice to get the contents of the remote picture back and then use strlen to compute the length, which requires downloading the pictures before you can compute them. If the picture is very large, then the network transmission will take a lot of time, the efficiency is obviously low. The author provides a method to improve efficiency, mainly using HTTP header information. When accessing a Web page, the server returns the requested header information, where content-length represents the size of the requested page content. If the request is a picture, then content-length represents the size of the picture. Based on this, just send the head request to get the returned header information OK. In PHP, you can get header information through the Fsockopen method. The code is as follows: code is as follows: $fp = Fsockopen ("www.baidu.com", $errno, $errstr, 30); if ($fp) { //The request is set to head on the line $out = "Head/img/baidu_sylogo1.gif http/1.1rn"; $ou T. = "HOST:WWW.BAIDU.COMRN"; $out. = "Connection:closernrn"; fwrite ($fp, $out); while (!feof ($fp)) { $header = fgets ($fp); if (strip OS ($header, ' Content-length ')!== false) { $size = Trim (substr ($header, Strpos ($ Header, ': ') + 1); echo $size; } { fclose ($FP); else { echo "$errstr ($errno)"; as well as initiating a GET request, just set the request type get to head. The requested host and path, modify it to their own needs on it. Summary: PHP can also use Get_headers to get header information, but the author tested this function, is a GET request, details Reference: PHP function get_headers is the head request or getting request. Other servers may block head requests, and if they are blocked, they can only use get requests honestly. If you want to do this, you can use the Out-of-the-box function getimagesize directly.