この関数を作成します。
<?php
getResourceSize($remoteFile /*link of the file*/)
{
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
if($status == ('404' || '500') return 'error';
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
return $contentLength;
}
}
?>
次に、合計ダウンロード サイズを次のように初期化します。
$files = array
[
"http://...firstFile.ext",
"http://...secondFile.ext",
"http://...thirdFile.ext",
...
];
$totalDownloadSize = 0;
foreach($file in $files)
$totalDownloadSize += GetResourceSize($file);