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 相关文章推荐
教你如何把一篇文章按要求分段
Oct 09 PHP
javascript 小型动画组件与实现代码
Jun 02 PHP
ThinkPHP3.1查询语言详解
Jun 19 PHP
destoon会员注册提示“数据校验失败(2)”解决方法
Jun 21 PHP
Sublime里直接运行PHP配置方法
Nov 28 PHP
phpQuery让php处理html代码像jQuery一样方便
Jan 06 PHP
PHP实现搜索地理位置及计算两点地理位置间距离的实例
Jan 08 PHP
Symfony2学习笔记之模板用法详解
Mar 17 PHP
PHP实现RTX发送消息提醒的实例代码
Jan 03 PHP
PHP chr()函数讲解
Feb 11 PHP
laravel添加前台跳转成功页面示例
Oct 22 PHP
laravel 查询数据库获取结果实现判断是否为空
Oct 24 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计算几分钟前、几小时前、几天前的几个函数、类分享
2014/04/09 PHP
php5.3后静态绑定用法详解
2016/11/11 PHP
基于laravel Request的所有方法详解
2019/09/29 PHP
PHP实现本地图片转base64格式并上传
2020/05/29 PHP
自己的js工具_Form 封装
2009/08/21 Javascript
关于JavaScript与HTML的交互事件
2013/04/12 Javascript
推荐JavaScript实现继承的最佳方式
2014/11/11 Javascript
jQuery实现多按钮单击变色
2014/11/27 Javascript
Jquery+Ajax+PHP+MySQL实现分类列表管理(上)
2015/10/28 Javascript
jquery制作图片时钟特效
2020/03/30 Javascript
Markdown+Bootstrap图片自适应属性详解
2016/05/21 Javascript
浅谈JavaScript for循环 闭包
2016/06/22 Javascript
微信小程序 toast 详解及实例代码
2016/11/09 Javascript
详解javascript中对数据格式化的思考
2017/01/23 Javascript
Vue.js展示AJAX数据简单示例讲解
2017/03/29 Javascript
JavaScript树的深度优先遍历和广度优先遍历算法示例
2018/07/30 Javascript
vue中选项卡点击切换且能滑动切换功能的实现代码
2018/11/25 Javascript
JS如何判断对象是否包含某个属性
2020/08/29 Javascript
微信小程序选择图片控件
2021/01/19 Javascript
[01:05:56]Liquid vs VP Supermajor决赛 BO 第二场 6.10
2018/07/04 DOTA
python嵌套函数使用外部函数变量的方法(Python2和Python3)
2016/01/31 Python
Python爬虫实现百度图片自动下载
2018/02/04 Python
tensorflow: variable的值与variable.read_value()的值区别详解
2018/07/30 Python
在Qt中正确的设置窗体的背景图片的几种方法总结
2019/06/19 Python
Python.append()与Python.expand()用法详解
2019/12/18 Python
python语言中有算法吗
2020/06/16 Python
英国百安居装饰建材网上超市:B&Q
2016/09/13 全球购物
如何强制垃圾回收
2015/10/06 面试题
党员自我批评与反省材料
2014/02/10 职场文书
文明之星事迹材料
2014/05/09 职场文书
教师听课评语大全
2014/12/31 职场文书
业务内勤岗位职责
2015/04/13 职场文书
防汛通知
2015/04/25 职场文书
消防演习感想
2015/08/10 职场文书
九年级历史教学反思
2016/02/19 职场文书
Python如何用re模块实现简易tokenizer
2022/05/02 Python