php获取远程文件大小的四种方法
时间:2016-08-19
我们都知道,获取本地文件大小可以使用php filesize()函数,但如何获取远程文件的大小呢?本文章向大家介绍php获取远程文件大小的四种方法,分别get_headers()方法、fsockopen()方法、curl_init方法和file_get_contents方法,需要的朋友可以参考一下。
get_headers()函数获取远程文件大小
get_headers() 是PHP系统级函数,他返回一个包含有服务器响应一个 HTTP 请求所发送的标头的数组。该数组中有一个值表示远程文件的大小,即Content-Length。
<?php
$url='http://www.manongjc.com';
print_r(get_headers($url));
?>
输出结果:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
fsockopen()方法获取远程文件大小
function getFileSize($url)
{
$url = parse_url($url);
if($fp = @fsockopen($url['host'],emptyempty($url['port'])?80:$url['port'],$error))
{
fputs($fp,"GET ".(emptyempty($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;
}
/* http://www.manongjc.com/article/1378.html */
elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))
{
return trim($arr[1]);
}
}
return null;
}
else
{
return null;
}
}
curl获取远程文件大小
curl也还比较方便,还能支持用户验证
function remote_filesize($uri,$user='',$pw='')
{
// start output buffering
ob_start();
// initialize curl with given uri
$ch = curl_init($uri);
// make sure we get the header
curl_setopt($ch, CURLOPT_HEADER, 1);
// make it a http HEAD request
curl_setopt($ch, CURLOPT_NOBODY, 1);
// if auth is needed, do it here
if (!emptyempty($user) && !emptyempty($pw))
{
$headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$okay = curl_exec($ch);
curl_close($ch);
// get the output buffer
$head = ob_get_contents();
// clean the output buffer and return to previous
// buffer settings
ob_end_clean();
echo '<br>head-->'.$head.'<----end <br>';
// gets you the numeric value from the Content-Length
// field in the http header
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
// if there was a Content-Length field, its value
// will now be in $matches[1]
if (isset($matches[1]))
{
$size = $matches[1];
}
else
{
$size = 'unknown';
}
//$last=round($size/(1024*1024),3);
//return $last.' MB';
return $size;
}
file_get_contents获取远程文件大小
$fCont = file_get_contents("http://www.weiyeying.cn/");
echo strlen($fCont)/1024;
- 我也来说说.net开源
- 是时候对员工进行网络安全培训了:黑客正将目标瞄准打印机
- 进度条ProgressBar
- Microsoft Visual Studio International Pack
- 柯洁5冠在手“食言”再战AI:我已看开 输赢无所谓
- JGulp: 利用Gulp 配置的前端项目自动化工作流
- 微软Enterprise Library 4.0将支持依赖注入
- 时钟AnalogClock与DigitalClock
- 细数那些在2017年被黑客滥用的系统管理工具和协议
- Compass: 在你的应用中集成搜索功能
- 列表选择Spinner
- 巧用CSS3 :target 伪类制作Dropdown下拉菜单(无JS)
- 开源的虚拟机软件 VirtualBox v1.5.2
- Farseer:一个用于Silverlight和XNA的开源物理引擎
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Android jni调试打印char阵列的实例详解
- 写JavaScript函数不得不知的高级技巧
- Android编程视频播放API之MediaPlayer用法示例
- Android实现点击缩略图放大效果
- Android 应用签名的两种方法
- Android 关闭多个Activity的实现方法
- 我从Vue源码中学到的一些JS编程技巧
- 组复制升级 | 全方位认识 MySQL 8.0 Group Replication
- 浅谈关于Android WebView上传文件的解决方案
- Android对图片Drawable实现变色示例代码
- 排序|优先队列不知道,先看看堆排序吧
- Android关于FTP文件上传和下载功能详解
- Android中封装RecyclerView实现添加头部和底部示例代码
- Python 十六进制hex-bytes-str之间的转换和Bcc码的生成
- android中实现手机号码的校验的示例代码