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 相关文章推荐
php5数字型字符串加解密代码
Apr 24 PHP
php 获取可变函数参数的函数
Aug 26 PHP
PHP连接SQLServer2005方法及代码
Dec 26 PHP
PHP IE中下载附件问题解决方法
Jan 07 PHP
PHP基于imap获取邮件实例
Nov 11 PHP
PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF
Feb 19 PHP
php 获取文件行数的方法总结
Oct 11 PHP
php redis实现对200w用户的即时推送
Mar 04 PHP
由php中字符offset特征造成的绕过漏洞详解
Jul 07 PHP
PHP简单实现循环链表功能示例
Nov 10 PHP
PHP中的empty、isset、isnull的区别与使用实例
Mar 22 PHP
PHP Ajax跨域问题解决方案代码实例
Aug 01 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读取ACCESS数据到MYSQL的代码
2011/05/11 PHP
PHP FTP操作类代码( 上传、拷贝、移动、删除文件/创建目录)
2014/05/10 PHP
在WordPress中获取数据库字段内容和添加主题设置菜单
2016/01/11 PHP
Jquery实现简单的轮播效果(代码管用)
2016/03/14 Javascript
jQuery移动端图片上传组件
2016/06/12 Javascript
Select下拉框模糊查询功能实现代码
2016/07/22 Javascript
JavaScript简单下拉菜单特效
2016/09/13 Javascript
Bootstrap时间选择器datetimepicker和daterangepicker使用实例解析
2016/09/17 Javascript
ES6概念 ymbol.for()方法
2016/12/25 Javascript
JS处理一些简单计算题
2018/02/24 Javascript
详解webpack打包时排除其中一个css、js文件或单独打包一个css、js文件(两种方法)
2018/10/26 Javascript
基于vue手写tree插件的那点事儿
2019/08/20 Javascript
原生js实现随机点餐效果
2019/12/10 Javascript
python集合类型用法分析
2015/04/08 Python
十个Python程序员易犯的错误
2015/12/15 Python
详解Python函数可变参数定义及其参数传递方式
2017/08/02 Python
python Pandas 读取txt表格的实例
2018/04/29 Python
Python实现的远程登录windows系统功能示例
2018/06/21 Python
pygame游戏之旅 按钮上添加文字的方法
2018/11/21 Python
VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法详解
2019/07/01 Python
python实现把两个二维array叠加成三维array示例
2019/11/29 Python
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
2020/03/02 Python
基于Keras中Conv1D和Conv2D的区别说明
2020/06/19 Python
Python实现迪杰斯特拉算法过程解析
2020/09/18 Python
python3列表删除大量重复元素remove()方法的问题详解
2021/01/04 Python
Reformation官网:美国女装品牌
2018/09/14 全球购物
会计毕业生自我鉴定
2013/11/04 职场文书
学习十八大精神心得体会
2013/12/31 职场文书
无故旷工检讨书
2014/01/26 职场文书
买房委托公证书
2014/04/08 职场文书
中班幼儿评语大全
2014/04/30 职场文书
2015安全保卫工作总结
2015/04/25 职场文书
公司车队管理制度
2015/08/04 职场文书
《亲亲我的妈妈》观后感(3篇)
2019/09/26 职场文书
Springboot/Springcloud项目集成redis进行存取的过程解析
2021/12/04 Redis
Spring Boot 底层原理基础深度解析
2022/04/03 Java/Android