$retornoTotal;
function getRemoteFilesize($link, $formatSize = true)
{
//echo "link que veio", $link;
$head = array_change_key_case(get_headers($link, 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
}
$links = array();
$lines = file("D:\\xampp\\htdocs\\scripts\\tamanho\\tamanho_por_links\\lista.txt");
foreach($lines as $line){
$links[] = $line;
}
for ($i = 0; $i < sizeof($links); $i++) {
$retornoTotal = getRemoteFilesize($links[$i]);
}
echo "Tamanho Total: ", $retornoTotal;我执行此脚本是为了检查外部服务器上文件的大小(使用url列表),并对每个文件的大小求和,返回总大小。问题是,这只是增加了第一个文件的大小,有人知道我哪里错了吗?示例:如果第一个文件大小为1MB,则其余文件的大小相同。因此,它会使list.txt文件中的链接总数增加1MB。
我的list.txt文件:
https://www.caco.ic.unicamp.br/manjaro/stable/core/x86_64/acl-2.2.53-3-x86_64.pkg.tar.zst
https://www.caco.ic.unicamp.br/manjaro/stable/core/x86_64/amd-ucode-20201023.r1747.dae4b4c-1-any.pkg.tar.zst
https://www.caco.ic.unicamp.br/manjaro/stable/core/x86_64/b43-fwcutter-019-3-x86_64.pkg.tar.xz发布于 2020-11-19 06:21:02
你每次都会覆盖$retornoTotal,你需要对它们求和。此外,您有一些冗余的代码,而foreach更简单:
$links = file("D:\\xampp\\htdocs\\scripts\\tamanho\\tamanho_por_links\\lista.txt");
$retornoTotal = 0;
foreach($links as $link) {
$retornoTotal += getRemoteFilesize($link);
}
echo "Tamanho Total: ", $retornoTotal;或者,如果您想要一个不同大小的数组,则将它们相加如下:
$links = file("D:\\xampp\\htdocs\\scripts\\tamanho\\tamanho_por_links\\lista.txt");
foreach($links as $link) {
$retorno[] = getRemoteFilesize($link);
}
$retornoTotal = array_sum($retorno);
echo "Tamanho Total: ", $retornoTotal;发布于 2020-11-25 06:25:39
已解决:
foreach($links as $link) {
$tamanho = $tamanho + remote_size(rtrim($link));
}
echo 'Bytes: ', $tamanho, '<br>';对我来说,只在foreach和解决方案中进行更改,其余的都是不必要的代码。谢谢你们所有人。
https://stackoverflow.com/questions/64902217
复制相似问题