php获取远程图片体积大小的实例


Posted in PHP onNovember 12, 2013

直接看代码吧,很好明白的

//用法 echo remote_filesize($url,$user='',$pw='');
$url = "http://www.aa.com/librarys/images/random/rand_11.jpg";//这里要换成你的图片地址
echo remote_filesize($url,$user='',$pw='');
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 (!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); // get the output buffer
    $head = ob_get_contents(); // clean the output buffer and return to previous // buffer settings
    ob_end_clean();  // 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_mb = round($size/(1024*1024),3);
 $last_kb = round($size/1024,3);
    return $last_kb . 'KB / ' . $last_mb.' MB';
}

函数的思路是,先CURL获取图片到缓冲区,然后正则获取图片的Content-Length信息就OK了。
PHP 相关文章推荐
substr()函数中文版
Oct 09 PHP
php 获取可变函数参数的函数
Aug 26 PHP
php将字符串随机分割成不同长度数组的方法
Jun 01 PHP
Yii2.0预定义的别名功能小结
Jul 04 PHP
mac系统下为 php 添加 pcntl 扩展
Aug 28 PHP
PHP二维数组去重算法
Dec 17 PHP
[原创]php正则删除html代码中class样式属性的方法
May 24 PHP
php递归函数怎么用才有效
Feb 24 PHP
thinkPHP框架动态配置用法实例分析
Jun 14 PHP
CentOS7.0下安装PHP5.6.30服务的教程详解
Sep 29 PHP
[原创]PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】
Jul 12 PHP
为你的 Laravel 验证器加上多验证场景的实现
Apr 07 PHP
php过滤XSS攻击的函数
Nov 12 #PHP
php获取新浪微博数据API实例
Nov 12 #PHP
php生成N个不重复的随机数实例
Nov 12 #PHP
三种php连接access数据库方法
Nov 11 #PHP
PHP中ob_start函数的使用说明
Nov 11 #PHP
PHP开发工具ZendStudio下Xdebug工具使用说明详解
Nov 11 #PHP
PHP利用str_replace防注入的方法
Nov 10 #PHP
You might like
透析PHP的配置文件php.ini
2006/10/09 PHP
ADODB类使用
2006/11/25 PHP
PHP错误提示的关闭方法详解
2013/06/23 PHP
关于file_get_contents返回为空或函数不可用的解决方案
2013/06/24 PHP
ini_set的用法介绍
2014/01/07 PHP
Zend Framework教程之Autoloading用法详解
2016/03/08 PHP
Thinkphp5行为使用方法汇总
2017/12/21 PHP
jQuery编辑器KindEditor4.1.4代码高亮显示设置教程
2013/03/01 Javascript
HTML上传控件取消选择
2013/03/06 Javascript
javascript常用函数(1)
2015/11/04 Javascript
jquery对复选框(checkbox)的操作汇总
2016/01/13 Javascript
详解Angular2中的编程对象Observable
2016/09/17 Javascript
JavaScript 详解预编译原理
2017/01/22 Javascript
基于JS代码实现简单易用的倒计时 x 天 x 时 x 分 x 秒效果
2017/07/13 Javascript
JavaScript for循环 if判断语句(学习笔记)
2017/10/11 Javascript
jQuery中过滤器的基本用法示例
2017/10/11 jQuery
基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果
2018/01/09 Javascript
JavaScript迭代器的含义及用法
2019/06/21 Javascript
JavaScript键盘事件响应顺序详解
2019/09/30 Javascript
JsonServer安装及启动过程图解
2020/02/28 Javascript
vue 路由缓存 路由嵌套 路由守卫 监听物理返回操作
2020/08/06 Javascript
使用Mock.js生成前端测试数据
2020/12/13 Javascript
python模块restful使用方法实例
2013/12/10 Python
利用PyInstaller将python程序.py转为.exe的方法详解
2017/05/03 Python
python实现超市扫码仪计费
2018/05/30 Python
Jacobi迭代算法的Python实现详解
2019/06/29 Python
python isinstance函数用法详解
2020/02/13 Python
Qoo10马来西亚:全球时尚和引领潮流的购物市场
2016/08/25 全球购物
PHP笔试题
2012/02/22 面试题
十佳护士获奖感言
2014/02/18 职场文书
环保口号大全
2014/06/12 职场文书
大学生交通专业求职信
2014/09/01 职场文书
2014七年级班主任工作总结
2014/12/05 职场文书
学校食品安全责任书
2015/01/29 职场文书
商务英语求职信范文
2015/03/19 职场文书
MySQL注入基础练习
2021/05/30 MySQL