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

在网上找了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元

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools