我想要一个PHP脚本或软件来确定放置在文本文件中的URL的文件大小 此脚本必须读取每行文本文件(每行是一个URL)并确定文件大小并最终计算整个URL的总大小 简单地说: 输入: site.com/lst.txt(网址列表) 输出: 文件大小//例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"
感谢
答案 0 :(得分:0)
向所有网址发送HEAD
- 请求,并为每个网址加总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);
答案 1 :(得分:0)
上面提到的代码工作得很好但是为了使用文本文件作为输入,并且它的每一行作为数组,我们可以将$urls
与FILE_IGNORE_NEW_LINES
的文本文件相等,以使每个url作为输入< / p>