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 相关文章推荐
PHPMailer安装方法及简单实例
Nov 25 PHP
PHP sprintf()函数用例解析
May 18 PHP
phpmailer中文乱码问题的解决方法
Apr 22 PHP
php对称加密算法示例
May 07 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
Nov 06 PHP
php下pdo的mysql事务处理用法实例
Dec 27 PHP
php mysql 封装类实例代码
Sep 18 PHP
PHP小偷程序的设计与实现方法详解
Oct 15 PHP
centos下file_put_contents()无法写入文件的原因及解决方法
Apr 01 PHP
详细解读php的命名空间(一)
Feb 21 PHP
php获取目录下所有文件及目录(多种方法)(推荐)
May 14 PHP
php查看一个变量的占用内存的实例代码
Mar 29 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
写一个用户在线显示的程序
2006/10/09 PHP
thinkphp3.2.3版本的数据库增删改查实现代码
2016/09/22 PHP
javascript while语句和do while语句的区别分析
2007/12/08 Javascript
jQuery验证Checkbox是否选中的代码 推荐
2011/09/04 Javascript
javascript时区函数介绍
2012/09/14 Javascript
讨论html与javascript在浏览器中的加载顺序问题
2013/11/27 Javascript
javascript中with()方法的语法格式及使用
2014/08/04 Javascript
jQuery实现长按按钮触发事件的方法
2015/02/02 Javascript
Nodejs关于gzip/deflate压缩详解
2015/03/04 NodeJs
JavaScript通过this变量快速找出用户选中radio按钮的方法
2015/03/23 Javascript
javascript元素动态创建实现方法
2015/05/13 Javascript
AngularJS基础 ng-dblclick 指令用法
2016/08/01 Javascript
Google 爬虫如何抓取 JavaScript 的内容
2017/04/07 Javascript
详解windows下vue-cli及webpack 构建网站(三)使用组件
2017/06/17 Javascript
jquery实现图片跟随鼠标的实例
2017/10/17 jQuery
原生JS实现简单的无缝自动轮播效果
2018/09/26 Javascript
利用JavaScript缓存远程窃取Wi-Fi密码的思路详解
2018/11/05 Javascript
Vue 路由间跳转和新开窗口的方式(query、params)
2019/12/25 Javascript
Python中使用glob和rmtree删除目录子目录及所有文件的例子
2014/11/21 Python
python将ip地址转换成整数的方法
2015/03/17 Python
Python装饰器用法实例总结
2018/05/26 Python
python 使用pdfminer3k 读取PDF文档的例子
2019/08/27 Python
Python 异常的捕获、异常的传递与主动抛出异常操作示例
2019/09/23 Python
Python编写一个验证码图片数据标注GUI程序附源码
2019/12/09 Python
python tkinter之顶层菜单、弹出菜单实例
2020/03/04 Python
Python使用pickle进行序列化和反序列化的示例代码
2020/09/22 Python
使用Python爬取小姐姐图片(beautifulsoup法)
2021/02/11 Python
Shopping happy life西班牙:以最优惠的价格提供最好的时尚配饰
2020/03/13 全球购物
介绍一下ICMP(Internet Control Message Protocol)Internet控制信息协议
2016/11/26 面试题
一道输出判断型Java面试题
2014/10/01 面试题
一年级家长会邀请函
2014/01/25 职场文书
2014报到证办理个人委托书
2014/10/08 职场文书
普宁寺导游词
2015/02/04 职场文书
2016中考冲刺决心书
2015/09/22 职场文书
SQL Server Agent 服务无法启动
2022/04/20 SQL Server
td 内容自动换行 table表格td设置宽度后文字太多自动换行
2022/12/24 HTML / CSS