-1

Okay, so I have a bit of a problem. I am using the HTML5 multiple file upload attribute to upload multiple files. I am using php to loop through each upload. I only want to run the foreach loo if the sum of all of the file sizes of the files being uploaded is greater that 0. How can I do this.

I have tried:

$file_test= $_FILES['uploads']['size']['0'];

if ($file_test != 0) {
   // code to run
}

But this ONLY tests the very first file.

Thanks!

4

5 回答 5

1
$totalSize = 0;
foreach ($_FILES['uploads']['size'] as $key => $val) {
    $totalSize += $val;
}
if ($totalSize > 0) {
    echo 'Execute stuff.';
}
于 2013-01-26T15:32:28.163 回答
0

This is basic PHP. Use a loop:

$file_test = 0;
$num_files = count($_FILES);
for ($i = 0; $i < $num_files; $i++) {
    $file_test += $_FILES['uploads']['size'][$i];
}

if ($file_test != 0) {
   // code to run
}
于 2013-01-26T15:31:09.090 回答
0

you need to loop over the entire file names array and find the total :

$totalSize = 0;
foreach ($_FILES['uploads']['name'] as $key => $value) {
    $totalSize += $_FILES['uploads']['size'][$key];
}
于 2013-01-26T15:32:03.863 回答
0

The PHP documentation actually has a big article about handling multiple file uploads. You can read it here. For your problem though, I would recommend looping through the $_FILES array like so:

for($x=0;$x<count($_FILES);$x++) {
     // Do whatever with $_FILES['uploads']['size'][$x]
}
于 2013-01-26T15:32:53.743 回答
0

So in your form you would have something like

<input type="file" name="uploads[]" />
<input type="file" name="uploads[]" />
<input type="file" name="uploads[]" />
...

then in PHP you can retrieve the files with

$file_test= $_FILES['uploads']['size'][0]; // for the first file
$file_test= $_FILES['uploads']['size'][1]; // for the second file
...

so lose the single quotes

That should do the trick,

Wezy

于 2013-01-26T15:33:31.727 回答