Home  >  Article  >  Backend Development  >  Get file size remotely

Get file size remotely

WBOY
WBOYOriginal
2016-07-29 09:01:381107browse
    /**
    *  远程获取文件大小
    */
    function getFileSize($url){
        $url = parse_url($url);//返回url组成部分关联数组
        if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)){
              fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");// 
              fputs($fp,"Host:$url[host]\r\n\r\n");
              while(!feof($fp)){
                   $tmp = fgets($fp);
                   if(trim($tmp) == ''){
                break;
                   }else if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)){
                    return trim($arr[1]);
                   }
              }
              return null;
        }else{
              return null;
        }
    }

Example: getFileSize

The above introduces the remote acquisition of file size, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn