一、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
image.png
二、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获取文件大小的几种方法!的更多相关文章

  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. go打印九九乘法表

    package main import ( "fmt" ) func main() { for i := 1; i < 10; i++ { for j := 1; j < ...

  2. sklearn.feature_extraction.text.CountVectorizer 学习

    CountVectorizer: CountVectorizer可以将文本文档集合转换为token计数矩阵.(token可以理解成词) 此实现通过使用scipy.sparse.csr_matrix产生 ...

  3. 为什么Java中只有值传递

    原文链接:https://www.cnblogs.com/wchxj/p/8729503.html 在开始深入讲解之前,有必要纠正一下大家以前的那些错误看法了.如果你有以下想法,那么你有必要好好阅读本 ...

  4. javascript策略模式的应用!

    最近在看<JavaScript设计模式与开发实践>这本书,受益匪浅,小记录一下书中的各个demo,加深理解: 策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替 ...

  5. yii2中通过migration创建数据表

    ### yii2中通过migration创建数据表 准备工作: 1.首先保证php写入了环境变量 2.在项目内创建migrations目录(base版的需要手动创建) 3.配置文件中正确配置了数据库信 ...

  6. Python 中文件操作

    上代码: import os import os.path rootdir = "d:/code/su/data" # 指明被遍历的文件夹 for parent,dirnames, ...

  7. Django 1.8.2 admin 数据库操作按下保存按钮出错

    Django报错:Runtimeerror: generator raised StopIteration python版本太新不兼容照成,下载python3.6就行了

  8. 2019牛客多校三 A. Graph Games (图分块)

    大意: 给定无向图, 定义$S(x)$为$x$的邻接点集合. 每次操作翻转$[L,R]$范围的边, 询问$S(x)$与$S(y)$是否相等. 快速判断集合相等可以用$CF 799F$的技巧. 采用$h ...

  9. SVM的概率输出(Platt scaling)

    SVM的概率输出(Platt scaling) 2015-10-22 10:38:19 闲渔Love吉他 阅读数 8121 文章标签: Platt Scaling Calibr 更多 分类专栏: 计算 ...

  10. Unity性能优化-DrawCall

    1. DrawCall是啥?其实就是对底层图形程序(比如:OpenGL ES)接口的调用,以在屏幕上画出东西.所以,是谁去调用这些接口呢?CPU.比如有上千个物体,每一个的渲染都需要去调用一次底层接口 ...