PHP获取文件大小的几种方法!
一、get_header($url,true):
- $url = 'http://www.xxx.com/MoJing_win_x86_64_V5.125.zip';
- $res = get_headers($url,true);
- echo "<pre>";
- print_R($res);
- die;
- $filesize = round($res['Content-Length']//,);//四舍五入获取文件大小,单位M

二、curl:
- $url = 'http://www.3d414.com/MoJing_win_x86_64_V5.125.zip';
- echo getFileSize($url);die;//
- function getFileSize($url,$user='',$pw='')
- {
- ob_start();
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_HEADER, );
- curl_setopt($ch, CURLOPT_NOBODY, );
- if (!empty($user) && !empty($pw))
- {
- $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- $okay = curl_exec($ch);
- curl_close($ch);
- $head = ob_get_contents();
- ob_end_clean();
- //echo '<br>head-->'.$head.'<----end <br>';
- $regex = '/Content-Length:\s([0-9].+?)\s/';
- $count = preg_match($regex, $head, $matches);
- $size = isset($matches[])?$matches[]:'unknown';
- //$last=round($size/(1024*1024),3);
- //return $last.' MB';
- return $size;
- }
三、fsockopen:
- $url = 'http://www.xxx.com/MoJing_win_x86_64_V5.125.zip';
- echo getFileSize($url);die;
- function getFileSize($url)
- {
- $url = parse_url($url);
- if($fp = @fsockopen($url['host'],empty($url['port'])?:$url['port'],$error))
- {
- fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
- fputs($fp,"Host:$url[host]\r\n\r\n");
- while(!feof($fp))
- {
- $tmp = fgets($fp);
- if(trim($tmp) == '')
- {
- break;
- }
- elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))
- {
- return trim($arr[]);
- }
- }
- return null;
- }
- else
- {
- return null;
- }
- }
filesize(),只能获取本地文件大小,不能获取远程文件大小:
- $url = './v3.rar';// 获取本地文件
- $url ='http://www.xxx.cc/v3.rar';//获取远程文件
- echo filesize($url);
file_get_contents(),获取文件内容,strlen()获取内容大小:
PHP获取文件大小的几种方法!的更多相关文章
- PHP中获取星期的几种方法
PHP中获取星期的几种方法 PHP星期几获取代码: 1 date(l); 2 //data就可以获取英文的星期比如Sunday 3 date(w); 4 //这个可以获取数字星期比如123,注意0 ...
- VC++获取IDC_EDIT的7种方法
VC++获取IDC_EDIT的7种方法 http://blog.csdn.net/baizengfei/article/details/7997618 //第一种方法 int number1, num ...
- C#获取当前路径的7种方法
总结C#获取当前路径的7种方法 C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName ...
- Java获取随机数的几种方法
Java获取随机数的几种方法 .使用org.apache.commons.lang.RandomStringUtils.randomAlphanumeric()取数字字母随机10位; //取得一个3位 ...
- spring 获取 WebApplicationContext的几种方法
spring 获取 WebApplicationContext的几种方法 使用ContextLoader WebApplicationContext webApplicationContext = C ...
- Struts2 后台获取路径的几种方法
Struts2 后台获取路径的几种方法 package actions.app; import java.io.File; import org.apache.struts2.ServletActio ...
- VC获取cookies的几种方法
方法一: CInternetSession::GetCookie This member function implements the behavior of the Win32 function ...
- 体温数据上传程序开发+获取时间的三种方法+DB Browser下载及安装
今天开始了体温上传程序的开发 今日所学: 获取时间 (21条消息) (转)安卓获取时间的三种方法_sharpeha的博客-CSDN博客_安卓获取时间 DB Browser安装教程 (20条消息) sq ...
- java 获取随机数的三种方法
方法1(数据类型)(最小值+Math.random()*(最大值-最小值+1))例:(int)(1+Math.random()*(10-1+1))从1到10的int型随数 方法2获得随机数for (i ...
随机推荐
- es6 装饰器decorator的使用 +webpack4.0配置
decorator 装饰器 许多面向对象都有decorator(装饰器)函数,比如python中也可以用decorator函数来强化代码,decorator相当于一个高阶函数,接收一个函数,返回一个被 ...
- [学习笔记] Blender 常用工具 移动与旋转,缩放, 变换
点击上面的移动图标之后,可在X,Y,Z轴移动物体. shift+S 之后,可有更多的移动选项. 旋转:可沿X,Y, Z 进行旋转 缩放 还可输入缩放的具体数值,更精确. 变换 可同时做移动.旋转.缩放 ...
- Django简介 --Python Web
Python Web主流的三种框架:Django.Flask.Tornado,使用频度:Django>Flask>Tornado 一.设计模式 MVC:模型(Model).View(视图) ...
- navicat-use
https://blog.csdn.net/weixin_44496987/article/details/87186071
- BinaryTree(HDU-5573)【思维/构造】
题目链接:https://vjudge.net/problem/HDU-5573 题意:一棵二叉树,编号代表对应节点的取值,可以走k步,每次走的层数递增,问能够达到N的方案. 思路:首先看一下数据范围 ...
- jenkinsFile harbor docker优化版
node { //study-center def mvnHome = tool name: 'maven', type: 'maven' REGISTRY = "192.168.22.27 ...
- (七)Action之ActionContext(OGNL表达式的使用)
一.ActionContext的重要性 struts中的数据都存放在ActionContext里,所以这部分是Action中的核心. ActionContext又称广义值栈,既然有广义值栈就有侠义值栈 ...
- (二十二)SpringBoot之使用mybatis generator自动生成bean、mapper、mapper xml
一.下载mybatis generator插件 二.生成generatorConfig.xml new一个generatorConfig.xml 三.修改generatorConfig.xml 里面的 ...
- internal关键字
internal修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问,接口的成员不能使用internal修饰符. 就是使用internal的类只能在同一个项目中使用,不能在别的项 ...
- MySQL 的COUNT(x)性能怎么样?
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! x 可以代表: 主键id.字段.1.* 0 说明 对于count(主键id)来说 innodb引擎会遍历整张表,把每一行的 ...