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

get file size of an url from text file that contain urls

我想要一个 php 脚本或软件来确定放置在文本文件中的 URL 的文件大小 此脚本必须读取文本文件的每一行(每一行都是一个 url)并确定文件大小并最终计算整个 url 的总大小 简单来说: 输入 : site.com/lst.txt(url 列表) 输出: 文件大小 // 示例 5.2G

这是我发现的可以计算 单个 url 文件大小的代码(如果可能的话)为我将其编辑为我上面提到的内容:

<?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"

谢谢

向所有 URL 发送 HEAD 请求,并对每个 URL 求和 Content-Length header。

使用您提供的代码:

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);

上面提到的代码运行良好,但为了使用文本文件作为输入并将其每一行作为数组,我们可以将 $urls 等同于带有 FILE_IGNORE_NEW_LINES 的文本文件以使每个 url 作为输入