【发布时间】:2015-08-25 16:56:38
【问题描述】:
我想要一个 php 脚本或软件来确定放置在文本文件中的 URL 的文件大小 该脚本必须读取文本文件的每一行(每一行都是一个 url)并确定文件大小并最终计算整个 url 的总大小 简单地说: 输入 : site.com/lst.txt(网址列表) 输出: 文件大小 // 例如 5.2G
这是我发现的可以计算 single 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 下载的数据的大小吗?
-
您是否尝试过解决这个问题?如果您有,请编辑您的问题以包含您的代码和研究,以显示对您不起作用的内容。如果你还没有,你应该先尝试自己解决它,然后在这里发布代码和研究。它也让其他人更容易回答您的问题!
-
是的,我想要从 url 下载文件的大小 -
-
我找到了一个确定单个 url 大小的代码,但这不是我想要的。我想计算每行保存在 txt 文件中的所有 url 的大小