kartik-v / yii2-widget-fileinput

An enhanced FileInput widget for Bootstrap 4.x/3.x with file preview, multiple selection, and more features (sub repo split from yii2-widgets)
Other
229 stars 96 forks source link

File sending error #164

Closed NLeone closed 4 years ago

NLeone commented 4 years ago

Hello dear, I have reviewed everywhere and I cannot find the error in another user, now I have also spent the whole afternoon trying to repair the error reproduced in my code and I cannot get it either, I have been using the widget for years but only now it gives me this error , and it is exactly with a newer version of yii2 and bootstrap 4:

Steps to reproduce the issue

  1. I add the widget to my form, from the beginning with use, and in the same form in the corresponding field as I always did, try different configurations here I will leave only the one suggested by the documentation, which is the most basic:

use kartik\file\FileInput;

$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);

echo $form->field($model, 'file_1')->widget(FileInput::class, [ 'options' => ['accept' => 'image/*']]);

echo $form->field($model, 'file_2')->widget(FileInput::class, [ 'options' => ['accept' => 'image/*']]);

  1. In the controller I receive $ _POST and I validate, then I load the instances of the files in the two corresponding attributes:
$model = new CoffeeLabel();

if ($model->load(Yii::$app->request->post()) && $model->validate())
{
    $model->file_1 = UploadedFile::getInstance($model, 'file_1');
    $model->file_2 = UploadedFile::getInstance($model, 'file_2');

    echo '<pre>';
    print_r($_FILES);
    print_r($_POST);
    echo '</pre>';
    exit;
  1. In the model I have well configured the validation rules for these attributes:

[['file_1', 'file_2'], 'image', 'extensions' => 'png, jpg, jpeg', 'message' => 'Extensión de archivo no permitida.'], [['file_1', 'file_2'], 'image', 'maxSize' => 2000000, 'tooBig' => 'El archivo {file} es demasiado grande.'],

Expected behavior and actual behavior

Expect you to send the files and be able to retrieve them in controller with class UploadedFile::getInstance($model, 'file_1'); and UploadedFile::getInstance($model, 'file_2');

As if you do it with the original fileInput() field of the framework, which I will show below.

The output I receive when I print $ _POST and $ _FILES is as follows on screen:

Array
(
)
Array
(
    [_csrf-backend] => 5LfuxbaOzPxGSvYk9HpeOVw7YjyFBqEc5Ug87FyIUaqX493o_-aZrQoEgVHZJRUJBG4metxOl06oe3uKJeMknA==
    [CoffeeLabel] => Array
        (
            [attr_1] => 6
            [attr_2] => 1
            [attr_3] => 8
            [attr_4] => 7
            [attr_5] => 3
            [attr_6] => 3
            [attr_7] => 10
            [attr_8] => 1
            [attr_9] => 2
            [attr_10] => 2
            [attr_11] => 2
            [attr_12] => 1
            [attr_13] => 1
            [attr_14] => 2
            [file_1] => 
            [file_2] => 
        )

)

The first two arrays are the empty ones, then there is obviously the model.

But if I do the exact same thing just by removing the widget fields from you and using the original from yii2, the answer is as follows:

Array
(
    [CoffeeLabel] => Array
        (
            [name] => Array
                (
                    [file_1] => lan-caihe-heritage-inset.jpg
                    [file_2] => tieguai-li-heritage-inset.jpg
                )

            [type] => Array
                (
                    [file_1] => image/jpeg
                    [file_2] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [file_1] => /tmp/phpkcf4jU
                    [file_2] => /tmp/phpFZ8XqR
                )

            [error] => Array
                (
                    [file_1] => 0
                    [file_2] => 0
                )

            [size] => Array
                (
                    [file_1] => 47514
                    [file_2] => 59577
                )

        )

)
Array
(
    [_csrf-backend] => vcD3fYNvrBuLSsKb3qBUa5sdvX_ZFurnyAUUyJTsq5XOlMRQygf5SscEte7z_x9bw0j5OYBe3LWFNlOu7Yfeow==
    [CoffeeLabel] => Array
        (
            [attr_1] => 6
            [attr_2] => 3
            [attr_3] => 5
            [attr_4] => 3
            [attr_5] => 6
            [attr_6] => 9
            [attr_7] => 10
            [attr_8] => 2
            [attr_9] => 2
            [attr_10] => 1
            [attr_11] => 2
            [attr_12] => 1
            [attr_13] => 1
            [attr_14] => 2
            [file_1] => 
            [file_2] => 
        )

)

Environment

Browsers

Operating System

Libraries

I have tried almost everything, please I need a little light about it since it seems to me a wonderful extension and I do not want to stop using it for an error that I am possibly making.

Cheers!

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/91624530-file-sending-error?utm_campaign=plugin&utm_content=tracker%2F8184669&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F8184669&utm_medium=issues&utm_source=github).
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.