I am querying mysql to get the id numbers of all entries made by a user. I am then using that array of numbers to get the file size of each corresponding file in a folder.

Example

id = 3
Image Name = 3.flv
size = 1.23

I need to get the combined file size of all the files that are listed.

    Could you explain some more? Do you want your first query

    I am querying mysql to get the id numbers of all entries made by a user.

    to get the file sizes as well?

      Are the file sizes stored in the DB, or are you having to calculate them on-the-fly (e.g. via [man]filesize/man) based on the SQL result set?

      Also... what's your question?

        I am calculating on the fly, which I have no problem doing individually. Each file name corresponds to the id of the row. I need to add them together for a total.

          If you answer the questions we better understand what the case is.

          Do you want a query that selects both the id and size or do you want a query that selects sizes based on the id's you got?

          I suppose that id, name and size is the columns in your database table?

            I am getting the file sizes on the fly with filesize(). I am able to get each separate file size in the loop but not a sum of all the sizes.

              mj9999;11002411 wrote:

              I am getting the file sizes on the fly with filesize(). I am able to get each separate file size in the loop but not a sum of all the sizes.

              I still dont know in what form you have the file paths. Do you have the paths in an array?

                I am just getting the size of each file within the loop.

                $result = mysql_query("SELECT * FROM videos WHERE user_id = '1'") 
                or die(mysql_error());  
                
                while($row = mysql_fetch_array( $result )) {
                
                $file = ''.$row['id'].'.flv';
                
                $path_file = $path.'/'.$file;
                
                $size = filesize($path_file);
                
                }
                  $totalSize = 0;
                  
                  $result = mysql_query("SELECT * FROM videos WHERE user_id = '1'")
                  or die(mysql_error());  
                  
                  while($row = mysql_fetch_array( $result )) {
                  
                  $file = ''.$row['id'].'.flv';
                  
                  $path_file = $path.'/'.$file;
                  
                  $totalSize += filesize($path_file);
                  
                  } 

                    Thanks but that still does not give me the total size of all the files.

                      mj9999;11002416 wrote:

                      Thanks but that still does not give me the total size of all the files.

                      Ok. But it should get you the total size of what you "grab" with the query you showed here?

                        I do get the size of each file but do not know how to get the cumulative size of all.

                          mj9999;11002418 wrote:

                          I do get the size of each file but do not know how to get the cumulative size of all.

                          Have you tryed to output the "$totalSize" in the code I gave you?

                            Yes, am now but it is returning all of the separate sizes. Not a sum of all.

                              mj9999;11002420 wrote:

                              Yes, am now but it is returning all of the separate sizes. Not a sum of all.

                              The function filesize is returning an integer for the size of the file so how "$totalSize" could output "all of the separate sizes" when adding integers is over my head....

                                Here is the output.

                                820965401344344223554709305548660210967818141462331484123014936079150809241531328316092265167335981755432723461255240452382504149825349399261673022691242427482594283012352922076129701970303579393059641131042021318088323216524633075057

                                Here it is with line breaks.

                                820965
                                4013443
                                4422355
                                4709305
                                5486602
                                10967818
                                14146233
                                14841230
                                14936079
                                15080924
                                15313283
                                16092265
                                16733598
                                17554327
                                23461255
                                24045238
                                25041498
                                25349399
                                26167302
                                26912424
                                27482594
                                28301235
                                29220761
                                29701970
                                30357939
                                30596411
                                31042021
                                31808832
                                32165246
                                33075057

                                  OK. Your "adding" is like you add strings like

                                  $string1 = '820965';
                                  $string2 = '4013443';
                                  echo $string1.$string2;
                                  
                                  // Thats a big differens to adding integers.
                                  
                                  $value1 = 820965;
                                  $value2 = 4013443;
                                  echo $value1 + $value2;
                                  

                                    If you put the sizes into an array

                                    $sizes[] = filesize($path_file);

                                    instead of

                                    $size=filesize($path_file);

                                    then (after) the loop,

                                    $total_size = array_total($sizes);

                                      Actually you'd use [man]array_sum[/man] since there is no array_total (at least not as part of PHP).

                                        $result = mysql_query("SELECT * FROM videos WHERE user_id = '1'")
                                        or die(mysql_error());

                                        while($row = mysql_fetch_array( $result )) {

                                        $file = ''.$row['id'].'.flv';

                                        $path_file = $path.'/'.$file;

                                        $size = filesize($path_file);

                                        }

                                        This is your code If the above code is not working for You you can take the size of all file in array like the below and just use the array_sum() to get the size of total file

                                        $result = mysql_query("SELECT * FROM videos WHERE user_id = '1'")
                                        or die(mysql_error());

                                        $total_size = array();
                                        while($row = mysql_fetch_array( $result )) {

                                        $file = ''.$row['id'].'.flv';

                                        $path_file = $path.'/'.$file;

                                        $total_size[] = $size = filesize($path_file);

                                        }
                                        $sum_of_filesize = array_sum($total_size);

                                        This will help You