Solution 1 :

First, if you are going to use += then you must initialise that variable before you do the first +=

Secondly, you have this line, in the loop, it will be setting the value of $totalFileSize to Zero, if the value exists and is > 0.

if (!empty($totalFileSize)){
    $totalFileSize = 0;
}

So change the code to

$totalFileSize = 0; // init before using `+=`

if (!empty($_FILES['attachment'])) { //If there are attachments
    $count = count($_FILES['attachment']['name']);
    if ($count > 0) {
        for ($i = 0; $i < $count; $i ++) {
            //Total File Size
            if ($_FILES["attachment"]["size"][$i] != 0){
                $totalFileSize += ($_FILES["attachment"]["size"][$i]);
            }
        }
        $message = $totalFileSize;
    }
    //Checks
    if ($totalFileSize > 20971520){
        $fileSizeError = 1;
    }
}

This could be simplified to

$totalFileSize = 0; // init before using `+=`

if (!empty($_FILES['attachment'])) { //If there are attachments
    foreach( $_FILES['attachment']['size'] as $size){
        //add up the File Sizes
        $totalFileSize += $size;
    }
    $message = $totalFileSize;
    //Checks
    if ($totalFileSize > 20971520){
        $fileSizeError = 1;
    }
}

Or even

if (!empty($_FILES['attachment'])) { //If there are attachments
    $totalFileSize = array_sum($_FILES['attachment']['size']){
    //Checks
    if ($totalFileSize > 20971520){
        $fileSizeError = 1;
    }
}

Or even

if (!empty($_FILES['attachment'])) { //If there are attachments
    if (array_sum($_FILES['attachment']['size']) > 20971520){
        $fileSizeError = 1;
    }
}

Problem :

I’ve got a HTML form that is posting form values and file uploads to PHP to process. One of the checks I want to do involved getting the total size of all files uploaded. This code I have below works fine if none of the values in the size array are 0, but if any of them are 0 (which happens when the file input is left blank) then the total always outputs as 0 for some reason.

Code:

    if (!empty($_FILES['attachment'])) { //If there are attachments
        $count = count($_FILES['attachment']['name']);
        if ($count > 0) {
            for ($i = 0; $i < $count; $i ++) {
                if (!empty($totalFileSize)){
                    $totalFileSize = 0;
                }
                //Total File Size
                if ($_FILES["attachment"]["size"][$i] != 0){
                    $totalFileSize += ($_FILES["attachment"]["size"][$i]);
                }
            }
            $message = $totalFileSize;
        }

        //Checks
        if ($totalFileSize > 20971520){
            $fileSizeError = 1;
        }
    }

    echo '<pre>'; print_r($_FILES['attachment']); echo '</pre>';
    echo $totalFileSize;
    exit();

Output:

Array
(
    [name] => Array
        (
            [0] => Image Today at 10_02_27.JPG
            [1] => 
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => 
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpfJo8Fl
            [1] => 
        )

    [error] => Array
        (
            [0] => 0
            [1] => 4
        )

    [size] => Array
        (
            [0] => 982518
            [1] => 0
        )

)
0

I can’t understand why the additional in the for loop works fine if there are no 0’s, but not when there is.

Comments

Comment posted by CBroe

if (!empty($totalFileSize)){ $totalFileSize = 0; }

Comment posted by CBroe

if ($_FILES["attachment"]["size"][$i] != 0){ $totalFileSize += ($_FILES["attachment"]["size"][$i]); }

By