从包含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头求和。

使用您提供的代码:

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

上面提到的代码工作得很好,但为了使用文本文件作为输入,它的每一行作为数组,我们可以将$urlsFILE_IGNORE_NEW_LINES相等的文本文件,使每个url作为输入