doudou130216 2015-08-25 16:56
浏览 241
已采纳

从包含url的文本文件中获取url的文件大小

i want an php script or software to determine file size of URLs placed in a text file this script have to read each line of text file (each line is a url) and determine file size and finaly calculate the total size of whole urls in simple : input : site.com/lst.txt (list of urls) output: size of files // example 5.2G

this is a code i found that can calculate single url file size if possible edit this for me to what i mentioned above :

<?php
/**
 *  Get the file size of any remote resource (using get_headers()), 
 *  either in bytes or - default - as human-readable formatted string.
 *
 *  @author  Stephan Schmitz <eyecatchup@gmail.com>
 *  @license MIT <http://eyecatchup.mit-license.org/>
 *  @url     <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d>
 *
 *  @param   string   $url          Takes the remote object's URL.
 *  @param   boolean  $formatSize   Whether to return size in bytes or formatted.
 *  @return  string                 Returns human-readable formatted size
 *                                  or size in bytes (default: formatted).
 *
 *  <code>
 *  //example
 *  echo getRemoteFilesize('https://github.com/eyecatchup/SEOstats/archive/master.zip');
 *  </code>
 */
function getRemoteFilesize($url, $formatSize = true)
{
    $head = array_change_key_case(get_headers($url, 1));
    // content-length of download (in bytes), read from Content-Length: field
    $clen = isset($head['content-length']) ? $head['content-length'] : 0;
    // cannot retrieve file size, return "-1"
    if (!$clen) {
        return -1;
    }
    if (!$formatSize) {
        return $clen; // return size in bytes
    }
    $size = $clen;
    switch ($clen) {
        case $clen < 1024:
            $size = $clen .' B'; break;
        case $clen < 1048576:
            $size = round($clen / 1024, 2) .' KiB'; break;
        case $clen < 1073741824:
            $size = round($clen / 1048576, 2) . ' MiB'; break;
        case $clen < 1099511627776:
            $size = round($clen / 1073741824, 2) . ' GiB'; break;
    }
    return $size; // return formatted size
}
$url = 'url_here';
echo getRemoteFilesize($url); // echoes "7.51 MiB"

thanks

  • 写回答

2条回答

  • douqian1296 2015-08-25 17:02
    关注

    Send a HEAD-request to all of the URLs and sum the Content-Length header for each of them.

    Using the code you provided:

    function getRemoteFilesize($url)
    {
        $head = array_change_key_case(get_headers($url, 1));
        // content-length of download (in bytes), read from Content-Length: field
        $clen = isset($head['content-length']) ? $head['content-length'] : 0;
        // cannot retrieve file size, return "-1"
        if (!$clen) {
            return -1;
        }
        return $clen; // return size in bytes
    }
    
    function formatBytes($clen) {
        $size = $clen;
        switch ($clen) {
            case $clen < 1024:
                $size = $clen .' B'; break;
            case $clen < 1048576:
                $size = round($clen / 1024, 2) .' KiB'; break;
            case $clen < 1073741824:
                $size = round($clen / 1048576, 2) . ' MiB'; break;
            case $clen < 1099511627776:
                $size = round($clen / 1073741824, 2) . ' GiB'; break;
        }
        return $size; // return formatted size
    }
    
    $urls = array('http://example.com', 'http://example.com', 'http://example.com', 'http://example.com');
    
    $sum = 0;
    
    for ($i=0; $i < count($urls); $i++) { 
        $res = getRemoteFilesize($urls[$i]);
        if ($res != -1) {
            $sum += $res;
        } else {
            echo 'content-length could not be retrieved for ' . $urls[$i];
        }
    }
    
    echo formatBytes($sum);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗