0

I know cURL can get the total download size of a page, but I am looking to divide the download size into the total images downloaded, total script size downloaded, total stylesheet size download etc etc.

What is the general way of going about doing this... I have found this link PHP: Remote file size without downloading file

and I am thinking it has something to do with doing a for loop and curl to get the size of each file pulled in by the inital curl request.

Let me know if anyone has any tips!

Thanks

Community
  • 1
  • 1
onei0120
  • 189
  • 1
  • 9
  • 28

1 Answers1

0

Make this function:

<?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;
      }
  }
?>

Now initialize your Total Download Size as:

$files = array
[
    "http://...firstFile.ext",
    "http://...secondFile.ext",
    "http://...thirdFile.ext",
    ...
];

 $totalDownloadSize = 0;

 foreach($file in $files)
     $totalDownloadSize += GetResourceSize($file);
Sean Vaughn
  • 3,792
  • 1
  • 16
  • 14