获取本地文件大小filesize()就可以了,但是如何获取远程文件的大小呢? 这里介绍三个方法来获取远程文件的大小.

方法1:get_headers

  1. <?php
  2. get_headers($url,true);
  3. //返回结果
  4. Array
  5. (
  6. [0] => HTTP/1.1 200 OK
  7. [Date] => Sat, 29 May 2004 12:28:14 GMT
  8. [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
  9. [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
  10. [ETag] => "3f80f-1b6-3e1cb03b"
  11. [Accept-Ranges] => bytes
  12. [Content-Length] => 438
  13. [Connection] => close
  14. [Content-Type] => text/html
  15. )
  16. ?>

此处可以直接根据Content-Length来获取到远程文件的大小了.

方法2:curl

  1. function remote_filesize($uri,$user='',$pw='')
  2. {
  3. // start output buffering
  4. ob_start();
  5. // initialize curl with given uri
  6. $ch = curl_init($uri);
  7. // make sure we get the header
  8. curl_setopt($ch, CURLOPT_HEADER, 1);
  9. // make it a http HEAD request
  10. curl_setopt($ch, CURLOPT_NOBODY, 1);
  11. // if auth is needed, do it here
  12. if (!emptyempty($user) && !emptyempty($pw))
  13. {
  14. $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
  15. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  16. }
  17. $okay = curl_exec($ch);
  18. curl_close($ch);
  19. // get the output buffer
  20. $head = ob_get_contents();
  21. // clean the output buffer and return to previous
  22. // buffer settings
  23. ob_end_clean();
  24. echo '<br>head-->'.$head.'<----end <br>';
  25. // gets you the numeric value from the Content-Length
  26. // field in the http header
  27. $regex = '/Content-Length:\s([0-9].+?)\s/';
  28. $count = preg_match($regex, $head, $matches);
  29. // if there was a Content-Length field, its value
  30. // will now be in $matches[1]
  31. if (isset($matches[1]))
  32. {
  33. $size = $matches[1];
  34. }
  35. else
  36. {
  37. $size = 'unknown';
  38. }
  39. //$last=round($size/(1024*1024),3);
  40. //return $last.' MB';
  41. return $size;
  42. }

方法3:socket

  1. function getFileSize($url)
  2. {
  3. $url = parse_url($url);
  4. if($fp = @fsockopen($url['host'],emptyempty($url['port'])?80:$url['port'],$error))
  5. {
  6. fputs($fp,"GET ".(emptyempty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
  7. fputs($fp,"Host:$url[host]\r\n\r\n");
  8. while(!feof($fp))
  9. {
  10. $tmp = fgets($fp);
  11. if(trim($tmp) == '')
  12. {
  13. break;
  14. }
  15. elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))
  16. {
  17. return trim($arr[1]);
  18. }
  19. }
  20. return null;
  21. }
  22. else
  23. {
  24. return null;
  25. }
  26. }

方法4:file_get_contents

  1. $fCont = file_get_contents("http://www.mg27.com/1.html");
  2. echo strlen($fCont)/1024;

以上四种方法
curl > fsock > file_get_contents > getheader

php获取远程文件大小的更多相关文章

  1. php下载远程大文件(获取远程文件大小)

    function download_file($url) { // $url = http://192.168.8.95/vm/download_file?downurl=http://192.168 ...

  2. PHP获取远程和本地文件信息(汇总)

    1.PHP filesize() 函数filesize() 函数返回指定文件的大小.若成功,则返回文件大小的字节数.若失败,则返回 false 并生成一条 E_WARNING 级的错误. 但是只能获取 ...

  3. php获取远程图片并把它保存到本地

    /* *功能:php多种方式完美实现下载远程图片保存到本地 *参数:文件url,保存文件名称,使用的下载方式 *当保存文件名称为空时则使用远程文件原来的名称 */ function getImage( ...

  4. 使用JCIFS获取远程共享文件

    package com.jadyer.util;  import java.io.File; import java.io.FileOutputStream; import java.io.IOExc ...

  5. curl获取远程图片存到本地

    $url = 'http://sssss/sss/xu0fLo9waqKSTDO7j0kSO41O5Luq3LB6ozUvY4O7OsXUWNicB49fBs8nGYzoqcwGDARQZHpVuic ...

  6. php 获取远程图片保存到本地

    php 获取远程图片保存到本地 使用两个函数 1.获取远程文件 2.把图片保存到本地 /** * 获取远程图片并把它保存到本地 * $url 是远程图片的完整URL地址,不能为空. */ functi ...

  7. 解析PHP中的file_get_contents获取远程页面乱码的问题【转】

    在工作中,遇到一个问题.我需要将一个网址(该网址是一个json数据的接口,即 打开该网址,在浏览器中显示的是json数据),我使用file_get_contents($url),数据是乱码的. 通过查 ...

  8. scp命令获取远程文件

    一.scp是什么? scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的,可能会稍微影响 ...

  9. PHP高效获取远程图片尺寸和大小(转)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

随机推荐

  1. Matlab求齐次方程的解

    % 求Ax=0的解: r=rank(A): x=null(A,r) 求出来x的是归一化后的解.

  2. Open-falon监控安装过程

    Open-falon监控安装过程   具体参考:   http://book.open-falcon.org/zh/quick_install/prepare.html 1. 安装ntp.vim编辑器 ...

  3. 基于SourceTree 下的 Git Flow 模型

    基于SourceTree 下的 Git Flow 模型 1. sourceTree  是一个开源的git 图形管理工具,可下载mac版本,windows版本 2. Git Flow 是一套使用Git进 ...

  4. EF Core » 影子属性

    Caution:注意 This documentation is for EF Core. For EF6.x and earlier release see http://msdn.com/data ...

  5. linux笔记:linux常用命令-文件搜索命令

    文件搜索命令:find(文件搜索) 一些示例: 注意:在以文件名为条件进行搜索时,支持通配符. 多条件搜索,以及直接对搜索到的文件进行操作: 文件搜索命令:locate(在文件资料库中查找文件) 文件 ...

  6. 数据库中间件mycat简单入门

    当在项目中mysql数据库成为瓶颈的时候,我们一般会使用主从复制,分库分表的方式来提高数据库的响应速度,比如mysql主从复制,在没有数据库中间件的情况下,我们只能由开发工程师在程序中控制,这对于一个 ...

  7. 一些简单css3效果的整理

    代码: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. 【转】使用Web墨卡托辅助球体切片方案的地图公共属性

    原文链接:https://doc.arcgis.com/en/data-appliance/6.1/reference/common-attributes.htm 使用Web墨卡托辅助球体切片方案的地 ...

  9. (26)odoo中的序列运用

    * 模块中增加序列    __openerp__.py :    ...     'data': [        'product_data.xml',    ],    ...    ------ ...

  10. MyBatis实体类映射文件模板

      <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC " ...