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