A
A
Alexander2016-02-21 13:19:18
Yii
Alexander, 2016-02-21 13:19:18

What is wrong (Uploading Multiple Files)?

view:

$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
echo $form->field($model, 'fileImage')->fileInput(['multiple' => true, 'accept' => 'image/*']);

Model :
public $fileImage;
    public function rules()
    {
        return [
            [['description_ru', 'description_en'], 'string', 'max' => 255],
            [['fileImage'], 'file', 'maxFiles' => 5, 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif'],
        ];
    }

Controller :
public function actionCreate()
    {
        $model = new Works();
        if ($model->load(Yii::$app->request->post())) {
            if($_FILES){
                echo '<pre>';
                print_r($_FILES);
                die();
            }
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

I select 3 pictures, I press create , but it shows one picture in the array
Array
(
    [Works] => Array
        (
            [name] => Array
                (
                    [fileImage] => ubuntu_bubbles_linux_93773_1366x768.jpg
                )
            [type] => Array
                (
                    [fileImage] => image/jpeg
                )
            [tmp_name] => Array
                (
                    [fileImage] => /tmp/phpT7u4pN
                )
            [error] => Array
                (
                    [fileImage] => 0
                )
            [size] => Array
                (
                    [fileImage] => 171551
                )
        )
)

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
T
ThunderCat, 2016-02-21
@ThunderCat

look at the source code of the form, maybe something went wrong,
'extensions' => 'png, jpg, jpeg, gif' - I can't say for sure, but maybe you need to specify them in upper case, the files that you uploaded do not have the .JPG format?
In php.ini there is a setting for the number of simultaneously loaded files. Although there is rarely less than 5 configured. Well, the size is also there, limited, see php_info ().

P
Paul Denisevich, 2016-02-21
@deniamnet

The name of the input should be with []at the end, as far as I know. For example, files[]. Then each file will be a separate element in this array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question