Home > php教程 > php手册 > php获取远程文件大小及信息的函数(header头信息获取)

php获取远程文件大小及信息的函数(header头信息获取)

WBOY
Release: 2016-06-21 08:57:32
Original
1988 people have browsed it

函数|文件大小|远程文件|header

php获取远程文件大小及信息的函数(header头信息获取)

阿里西西Alixixi.com开发团队在做一个客户系统时,需要做远程下载的功能,并实时显示进度条效果。

所以,需要预先读取远程文件的大小信息,然后做为实时下载进度条的参数。

功能函数如下,调用很简单,getFileSize("http://www.alixixi.com/download/xml.rar") ,就可以获取远程文件的大小了。

以下是引用片段:
function getFileSize($url){ 
        $url = parse_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;
        }
}
echo getFileSize("http://www.alixixi.com/download/xml.rar")
?>

此函数获取远程文件大小的效果非常好,无论远程文件体积有多大,获取速度完全不受影响。



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template