首页 > Web开发 > 详细

PHP 获取远程文件大小的3种解决方法

时间:2017-04-27 12:16:20      阅读:219      评论:0      收藏:0      [点我收藏+]
转 http://www.3lian.com/edu/2013/07-12/80472.html
以下是对PHP中获取远程文件大小的3种解决方法进行了详细的介绍,需要的朋友参考下
 

1、使用file_get_contents()

复制代码 代码如下:

<?php
$file = file_get_contents($url);
echo strlen($file);
?>


2. 使用get_headers()

复制代码 代码如下:

<?php
$header_array = get_headers($url, true);
$size = $header_array[‘Content-Length‘];
echo $size;
?>


PS:
需要打开allow_url_fopen!
如未打开会显示
Warning: get_headers() [function.get-headers]: URL file-access is disabled in the server configuration
3.使用fsockopen()

复制代码 代码如下:


<?php
 function get_file_size($url) {
     $url = parse_url($url);

     if (empty($url[‘host‘])) {
         return false;
     }

     $url[‘port‘] = empty($url[‘post‘]) ? 80 : $url[‘post‘];
     $url[‘path‘] = empty($url[‘path‘]) ? ‘/‘ : $url[‘path‘];

     $fp = fsockopen($url[‘host‘], $url[‘port‘], $error);

     if($fp) {
         fputs($fp, "GET " . $url[‘path‘] . " HTTP/1.1rn");
         fputs($fp, "Host:" . $url[‘host‘]. "rnrn");

         while (!feof($fp)) {
             $str = fgets($fp);
             if (trim($str) == ‘‘) {
                 break;
             }elseif(preg_match(‘/Content-Length:(.*)/si‘, $str, $arr)) {
                 return trim($arr[1]);
             }
         }
         fclose ( $fp);
         return false;
     }else {
         return false;
     }
 }
 ?>

PHP 获取远程文件大小的3种解决方法

原文:http://www.cnblogs.com/sensai-sun/p/6773536.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!