一、get_header($url,true):
  1. $url = 'http://www.xxx.com/MoJing_win_x86_64_V5.125.zip';
  2. $res = get_headers($url,true);
  3. echo "<pre>";
  4. print_R($res);
  5. die;
  6. $filesize = round($res['Content-Length']//,);//四舍五入获取文件大小,单位M
image.png
二、curl:
  1. $url = 'http://www.3d414.com/MoJing_win_x86_64_V5.125.zip';
  2. echo getFileSize($url);die;//
  3. function getFileSize($url,$user='',$pw='')
  4. {
  5. ob_start();
  6. $ch = curl_init($url);
  7. curl_setopt($ch, CURLOPT_HEADER, );
  8. curl_setopt($ch, CURLOPT_NOBODY, );
  9. if (!empty($user) && !empty($pw))
  10. {
  11. $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
  12. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  13. }
  14. $okay = curl_exec($ch);
  15. curl_close($ch);
  16. $head = ob_get_contents();
  17. ob_end_clean();
  18.  
  19. //echo '<br>head-->'.$head.'<----end <br>';
  20.  
  21. $regex = '/Content-Length:\s([0-9].+?)\s/';
  22. $count = preg_match($regex, $head, $matches);
  23. $size = isset($matches[])?$matches[]:'unknown';
  24. //$last=round($size/(1024*1024),3);
  25. //return $last.' MB';
  26. return $size;
  27. }
三、fsockopen:
  1. $url = 'http://www.xxx.com/MoJing_win_x86_64_V5.125.zip';
  2. echo getFileSize($url);die;
  3. function getFileSize($url)
  4. {
  5. $url = parse_url($url);
  6. if($fp = @fsockopen($url['host'],empty($url['port'])?:$url['port'],$error))
  7. {
  8. fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
  9. fputs($fp,"Host:$url[host]\r\n\r\n");
  10. while(!feof($fp))
  11. {
  12. $tmp = fgets($fp);
  13. if(trim($tmp) == '')
  14. {
  15. break;
  16. }
  17. elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))
  18. {
  19. return trim($arr[]);
  20. }
  21. }
  22. return null;
  23. }
  24. else
  25. {
  26. return null;
  27. }
  28. }
filesize(),只能获取本地文件大小,不能获取远程文件大小:
  1. $url = './v3.rar';// 获取本地文件
  2. $url ='http://www.xxx.cc/v3.rar';//获取远程文件
  3. echo filesize($url);
file_get_contents(),获取文件内容,strlen()获取内容大小:

PHP获取文件大小的几种方法!的更多相关文章

  1. PHP中获取星期的几种方法

    PHP中获取星期的几种方法   PHP星期几获取代码: 1 date(l); 2 //data就可以获取英文的星期比如Sunday 3 date(w); 4 //这个可以获取数字星期比如123,注意0 ...

  2. VC++获取IDC_EDIT的7种方法

    VC++获取IDC_EDIT的7种方法 http://blog.csdn.net/baizengfei/article/details/7997618 //第一种方法 int number1, num ...

  3. C#获取当前路径的7种方法

    总结C#获取当前路径的7种方法 C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName ...

  4. Java获取随机数的几种方法

    Java获取随机数的几种方法 .使用org.apache.commons.lang.RandomStringUtils.randomAlphanumeric()取数字字母随机10位; //取得一个3位 ...

  5. spring 获取 WebApplicationContext的几种方法

    spring 获取 WebApplicationContext的几种方法 使用ContextLoader WebApplicationContext webApplicationContext = C ...

  6. Struts2 后台获取路径的几种方法

    Struts2 后台获取路径的几种方法 package actions.app; import java.io.File; import org.apache.struts2.ServletActio ...

  7. VC获取cookies的几种方法

    方法一: CInternetSession::GetCookie This member function implements the behavior of the Win32 function  ...

  8. 体温数据上传程序开发+获取时间的三种方法+DB Browser下载及安装

    今天开始了体温上传程序的开发 今日所学: 获取时间 (21条消息) (转)安卓获取时间的三种方法_sharpeha的博客-CSDN博客_安卓获取时间 DB Browser安装教程 (20条消息) sq ...

  9. java 获取随机数的三种方法

    方法1(数据类型)(最小值+Math.random()*(最大值-最小值+1))例:(int)(1+Math.random()*(10-1+1))从1到10的int型随数 方法2获得随机数for (i ...

随机推荐

  1. es6 装饰器decorator的使用 +webpack4.0配置

    decorator 装饰器 许多面向对象都有decorator(装饰器)函数,比如python中也可以用decorator函数来强化代码,decorator相当于一个高阶函数,接收一个函数,返回一个被 ...

  2. [学习笔记] Blender 常用工具 移动与旋转,缩放, 变换

    点击上面的移动图标之后,可在X,Y,Z轴移动物体. shift+S 之后,可有更多的移动选项. 旋转:可沿X,Y, Z 进行旋转 缩放 还可输入缩放的具体数值,更精确. 变换 可同时做移动.旋转.缩放 ...

  3. Django简介 --Python Web

    Python Web主流的三种框架:Django.Flask.Tornado,使用频度:Django>Flask>Tornado 一.设计模式 MVC:模型(Model).View(视图) ...

  4. navicat-use

    https://blog.csdn.net/weixin_44496987/article/details/87186071

  5. BinaryTree(HDU-5573)【思维/构造】

    题目链接:https://vjudge.net/problem/HDU-5573 题意:一棵二叉树,编号代表对应节点的取值,可以走k步,每次走的层数递增,问能够达到N的方案. 思路:首先看一下数据范围 ...

  6. jenkinsFile harbor docker优化版

    node { //study-center def mvnHome = tool name: 'maven', type: 'maven' REGISTRY = "192.168.22.27 ...

  7. (七)Action之ActionContext(OGNL表达式的使用)

    一.ActionContext的重要性 struts中的数据都存放在ActionContext里,所以这部分是Action中的核心. ActionContext又称广义值栈,既然有广义值栈就有侠义值栈 ...

  8. (二十二)SpringBoot之使用mybatis generator自动生成bean、mapper、mapper xml

    一.下载mybatis generator插件 二.生成generatorConfig.xml new一个generatorConfig.xml 三.修改generatorConfig.xml 里面的 ...

  9. internal关键字

    internal修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问,接口的成员不能使用internal修饰符. 就是使用internal的类只能在同一个项目中使用,不能在别的项 ...

  10. MySQL 的COUNT(x)性能怎么样?

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! x 可以代表: 主键id.字段.1.* 0 说明 对于count(主键id)来说 innodb引擎会遍历整张表,把每一行的 ...