Home > php教程 > php手册 > php获取文件大小的4种方法

php获取文件大小的4种方法

WBOY
Release: 2016-06-07 11:43:30
Original
3160 people have browsed it

在网上找了4种获取文件大小的方法 记录起来。。。。
方法一:header<?php <br /> get_headers($url,true);   <br>    <br> //返回结果    <br> Array   <br> (   <br>     [0] => HTTP/1.1 200 OK   <br>     [Date] => Sat, 29 May 2004 12:28:14 GMT   <br>     [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)   <br>     [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT   <br>     [ETag] => "3f80f-1b6-3e1cb03b"   <br>     [Accept-Ranges] => bytes   <br>     <strong>[Content-Length] => 438 </strong>  <br>     [Connection] => close   <br>     [Content-Type] => text/html   <br> )   <br> ?>   

方法二function getFileSize($url)   <br> {   <br>     $url = parse_url($url);   <br>     if($fp = @fsockopen($url['host'],emptyempty($url['port'])?80:$url['port'],$error))   <br>     {   <br>         fputs($fp,"GET ".(emptyempty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");   <br>         fputs($fp,"Host:$url[host]\r\n\r\n");   <br>         while(!feof($fp))   <br>         {   <br>             $tmp = fgets($fp);   <br>             if(trim($tmp) == '')   <br>             {   <br>                 break;   <br>             }   <br>             elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))   <br>             {   <br>                 return trim($arr[1]);   <br>             }   <br>         }   <br>         return null;   <br>     }   <br>     else   <br>     {   <br>         return null;   <br>     }   <br> }  方法三:function remote_filesize($uri,$user='',$pw='')   <br> {   <br>     // start output buffering    <br>     ob_start();   <br>     // initialize curl with given uri    <br>     $ch = curl_init($uri);   <br>     // make sure we get the header    <br>     curl_setopt($ch, CURLOPT_HEADER, 1);   <br>     // make it a http HEAD request    <br>     curl_setopt($ch, CURLOPT_NOBODY, 1);   <br>     // if auth is needed, do it here    <br>     if (!emptyempty($user) && !emptyempty($pw))   <br>     {   <br>         $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));   <br>         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);   <br>     }   <br>     $okay = curl_exec($ch);   <br>     curl_close($ch);   <br>     // get the output buffer    <br>     $head = ob_get_contents();   <br>     // clean the output buffer and return to previous    <br>     // buffer settings    <br>     ob_end_clean();   <br>    <br>     echo '<br>head-->'.$head.'';   <br>    <br>     // gets you the numeric value from the Content-Length    <br>     // field in the http header    <br>     $regex = '/Content-Length:\s([0-9].+?)\s/';   <br>     $count = preg_match($regex, $head, $matches);   <br>    <br>     // if there was a Content-Length field, its value    <br>     // will now be in $matches[1]    <br>     if (isset($matches[1]))   <br>     {   <br>         $size = $matches[1];   <br>     }   <br>     else   <br>     {   <br>         $size = 'unknown';   <br>     }   <br>     //$last=round($size/(1024*1024),3);    <br>     //return $last.' MB';    <br>     return $size;   <br> }   方法四:$fCont = file_get_contents("http://www.weiyeying.cn/");   <br> echo strlen($fCont)/1024;  

AD:真正免费,域名+虚机+企业邮箱=0元

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