Sum total of upload size for any given record

4 posts by 2 authors in: Forums > CMS Builder
Last Post: February 7, 2013   (RSS)

By gregThomas - February 6, 2013

Hi,

As far as I know, that is currently the best way to get the size of a file. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By dm - February 7, 2013

hi greg

this works great to get the filesize of each individual upload within a record, but i was wondering if there is similar code for the total size of all the uploads within a record ie if ten uploads have been uploaded what is the sum total of those ten not just individually if you follow...

cheers!

By gregThomas - February 7, 2013

Sorry, I didn't read the question properly.

The easiest way to get the sum total of your uploads is to loop through them and add the file sizes together:

  //This will contain our total file size.
  $totalSize = 0;

  //Loop through each record and add its file size to the totalSize variable
  foreach($blogRecords[0]['uploads'] as $upload)  $totalSize += filesize($upload['filePath']);

  //Convert to MB
  $totalSize = round($totalSize * .0009765625 * .0009765625, 2);

  //Display result
  echo $totalSize."MB";

You'll need to change the initial variable in the foreach loop to use the variable that contains your array of images.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com