Read HTML5 Multiple File Input from PHP

Ok, so you have a HTML 5 file input markup on your view and you have enabled multiple attribute to true. You may also be using widgets based on HTML5 input like \kartik\widgets\FileInput. But when you read a file in PHP Server code using $_FILES you do not see an array of files. Similarly, when using a PHP framework like Yii, in the controller using CUploadedFile (Yii1) or
UploadedFile (Yii2), you do not see a list of multiple files that you selected in your view, but only one row.

File Input Markup

Your HTML markup in your view is something like this and you don’t know why this is not working?

<input name="uploadFile" type="file" multiple="multiple">

Solution

  1. You must set your form’s encoding type to multipart/form data (enctype="multipart/form-data") for file uploads.
  2. You must configure the attribute name for HTML5 file input in ARRAY FORMAT for PHP to recognize it as an array of files.

So here is how you achieve this as shown below (note the square brackets in the attribute naming):

Your Form Layout

For Generic PHP/HTML

<form enctype="multipart/form-data" action="file-upload.php" method="POST">
<input name="uploadFile[]" type="file" class="file" multiple="multiple">
</form>

For Yii 1.1

$form = $this->beginWidget('CActiveForm', array(
    'options'=>['encytype'=>'multipart/form-data']
));
echo $form->fileInput($model, 'uploadFile[]', array('multiple' => true));
$this->endWidget();

For Yii 2

use \yii\widgets\ActiveForm;
$form = ActiveForm::begin([
    'options'=>['encytype'=>'multipart/form-data']
]);
echo $form->field($model, 'uploadFile[]')->fileInput(['multiple' => true]);
ActiveForm::end;

For \kartik\widgets\FileInput (Yii 2)

use \yii\widgets\ActiveForm;
$form = ActiveForm::begin([
    'options'=>['encytype'=>'multipart/form-data']
]);
echo $form->field($model, 'uploadFile[]')->widget(FileInput::classname(), [
    'options'=>['multiple' => true]
]);
ActiveForm::end;

Now when you fetch your uploaded files after form submission using $_FILES['uploadFile'], you should receive an array of files for processing.

// PHP Server Code to process submitted form
$numUploadedfiles = count($_FILES['uploadFile']);
for($i = 0; $i < $numUploadedfiles; $i++)
{
    echo "<br>filename " . $i . " is: " . $_FILES['uploadFile'][$i];
    // or do whatever
}

4 thoughts on “Read HTML5 Multiple File Input from PHP

  1. A mi me funcionó asi!:

    tuve problemas con el código siguiente en mi controlador:

    // PHP Server Code to process submitted form
    $numUploadedfiles = count($_FILES[‘uploadFile’]);
    for($i = 0; $i < $numUploadedfiles; $i++)
    {
    echo “filename ” . $i . ” is: ” . $_FILES[‘uploadFile’][$i];
    // or do whatever
    }

    y es que al momento de imprimir el Array: $_FILES[‘uploadFile’], salia lo siguiente:

    Array
    (
    [name] => Array
    (
    [0] => Calvin_klein_logo.png
    [1] => photo.png
    )

    [type] => Array
        (
            [0] => image/png
            [1] => image/png
        )
    
    [tmp_name] => Array
        (
            [0] => C:\xampp\tmp\php54CC.tmp
            [1] => C:\xampp\tmp\php54CD.tmp
        )
    
    [error] => Array
        (
            [0] => 0
            [1] => 0
        )
    
    [size] => Array
        (
            [0] => 36625
            [1] => 99397
        )
    

    )

    es decir la variable $numUploadedfiles siempre arroja el valor ‘5’, es por ello que lo cambie y me funciono:

    $numUploadedfiles = count($_FILES[‘uploadFile’]);

            for($i = 0; $i < $numUploadedfiles; $i++)
            {
                echo "<br>filename " . $i . " is: " . $_FILES['uploadFile']['name'][$i];
                // or do whatever
            }.
    

    Saludos

Leave a Reply