0

I'm trying to upload multiple files, by altering a script I once used to upload a single file.

The only changes I've made are to add more file input fields to the form. But when I submit the form to the script, it halts at where I check for errors. How do I proceed with multiple files?

$form  = '<form action="imgProcess.php" method="post" enctype="multipart/form-data">';
$form .= '<div><input type="file" name="file[]"></div>';
$form .= '<div><input type="file" name="file[]"></div>';
$form .= '<div><input type="file" name="file[]"></div>';
$form .= '<div><input type="file" name="file[]"></div>';
$form .= '<div><input type="file" name="file[]"></div>';
$form .= '<input type="hidden" name="handle" value="testHandle" />';
$form .= '<input type="submit">';
$form .= '</form>';

imgProcess.php

if($_FILES && isset($_POST['handle'])) {

    $numFiles = count($_FILES['file']['tmp_name']);

    echo $numFiles;

    $handle = $_POST['handle'];

    echo $handle;

    // Halt on errors
    if($_FILES['file']['error'] == 0) {


    } else {
        echo 'There were errors<br>';
        print_r( $_FILES['file']['error'] );
    }
} else {
    echo 'Error 1';
}

//
There were errors
Array ( [0] => 0 [1] => 0 [2] => 4 [3] => 4 [4] => 4 )
4
  • So, where are the files supposed to be "uploaded" to? Are you not using move_uploaded_file()? Commented Aug 21, 2014 at 6:49
  • That comes later in the script... First check for errors, then proceed. that's how I do it... Commented Aug 21, 2014 at 6:51
  • Have a look at the Q&A stackoverflow.com/q/9046657 found under "Related". You may spend less time using another script. You'll definitely need a foreach loop. Commented Aug 21, 2014 at 6:54
  • Ah! Didn't see your comment. Commented Aug 21, 2014 at 7:17

4 Answers 4

2

You have 5 elements of file.

Whereas the status-code in error msg you have printed shows that you have uploaded only two files.

Array ( [0] => 0 [1] => 0 [2] => 4 [3] => 4 [4] => 4 )
                                 ^        ^        ^

Rest of them were kept empty and you submitted the form. Refer this to understand the details.

This is what is giving error. You might have to handle it differently so that it considers only the files that you have uploaded.

Sign up to request clarification or add additional context in comments.

1 Comment

You're correct, I uploaded only 2. So error 4 says no file was uploaded. I'm totally new to this file uploading thing. How do I handle this? $numFiles is always 5 even if I upload 2.
1

you can add attribute "multiple" to your input field, instead of adding many input fields. in official php site you can find example how upload multiple files

Comments

1

from all files if you want to upload those files that do not have any error then use try and catch and if you want to throw error if any one of them is having error then change your condition to check error like this

$fileUploadError = array_filter($_FILES['file']['error']);

if(count($fileUploadError) > 0 ) { 

     echo "error";    
} else {

     echo "no error";    
}

hope this might help you

Comments

0

Please try this

    $error=$_FILES['file']['error'];

for($i=0;$i<$numFiles;$i++)
{
     if($error[$i] == 0) {

        //your code
    } 
}

2 Comments

For some strange reason, $numFiles is always 5 even if I upload 2 files. Any way to get around this?
$numFiles = count($_FILES['file']['tmp_name']); This will count all the inputs you have. eventhough no file is uploaded use if($_FILES['file']['size'] > 0) to check whether a file is uploaded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.