从包含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

dsqa6272
dsqa6272 我找到了一个确定单个网址大小的代码,但这不是我想要的。我想计算每行中保存在txt文件中的所有网址的大小
大约 5 年之前 回复
doujian1954
doujian1954 是的我想要从网址下载文件的大小-
大约 5 年之前 回复
douren8379
douren8379 你试图解决这个问题吗?如果您有,请编辑您的问题以包含您的代码和研究,以显示哪些内容对您没有用。如果还没有,你应该先尝试自己解决,然后在这里发布代码和研究。它让你的问题更容易让别人回答!
大约 5 年之前 回复
dongxing2015
dongxing2015 您是指从这些网址下载的数据的大小?
大约 5 年之前 回复

2个回答

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);
dpwtr666638
dpwtr666638 请标记答案接受:)
大约 5 年之前 回复
doulin2029
doulin2029 谢谢它的工作。 我将代码更改为我需要的并发送结果
大约 5 年之前 回复
dtpd58676
dtpd58676 我不是php程序员......
大约 5 年之前 回复



上面提到的代码工作得很好但是为了使用文本文件作为输入,它的每一行作为数组,我们可以等于 $ urls </ code>到 FILE_IGNORE_NEW_LINES </ code>的文本文件,使每个网址成为输入</ p>
</ div>

展开原文

原文

that code mentioned above is working nice but in order to use text file as input and each line of it as array we can equal $urls to text file with FILE_IGNORE_NEW_LINES to make each url as input

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐