Home > php教程 > php手册 > php4如何获取远程文件大小类

php4如何获取远程文件大小类

WBOY
Release: 2016-06-13 11:24:08
Original
1161 people have browsed it

php5有get_headers非常好用,因为他可以直接读取远程文件的信息,而不用把整个文件读取到服务器。但是现在很多web服务器都不支持php5,下面我们就来看一个关于php4获取远程服务器文件大小的实例类吧。

php教程4如何获取远程文件大小类
 /*
 php5有get_headers非常好用,因为他可以直接读取远程文件的信息,而不用把整个文件读取到服务器。但是现在很多web服务器都不支持php5,下面我们就来看一个关于php4获取远程服务器文件大小的实例类吧。
 */

if(!function_exists('get_headers'))
{
    function get_headers($url,$format=0)
    {
        $url=parse_url($url);
        $end = "rnrn";
        $fp = fsockopen($url['host'], (empty($url['port'])?80:$url['port']), $errno, $errstr, 30);
        if ($fp)
        {
            $out  = "get / http/1.1rn";
            $out .= "host: ".$url['host']."rn";
            $out .= "connection: closernrn";
            $var  = '';
            fwrite($fp, $out);
            while (!feof($fp))
            {
                $var.=fgets($fp, 1280);
                if(strpos($var,$end))
                    break;
            }
            fclose($fp);

            $var=preg_replace("/rnrn.*$/",'',$var);
            $var=explode("rn",$var);
            if($format)
            {
                foreach($var as $i)
                {
                    if(preg_match('/^([a-za-z -]+): +(.*)$/',$i,$parts))
                        $v[$parts[1]]=$parts[2];
                }
                return $v;
            }
            else
                return $var;
        }
    }
}


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