3

I am trying to upload a image by a form to my Model. I am using Yii 1.1

The model doesn't contain in database the column "image", so I have created the attribute in the model like this

public $image;

I have added in rules the following

array('image', 'file', 'types'=>'jpg,gif,png', 'allowEmpty'=>true),

In the view I added to the form the htmlOption array('enctype' => 'multipart/form-data'), while for the file upload:

<th><?php echo $form->labelEx($model, 'image'); ?></th>
<td><?php echo $form->fileField($model, 'image');?></td>
<th><?php echo $form->error($model, 'image');'>'?></th>

I see the button for uploading the picture, when I press the button the action of my controller is called but if I make a log for showing the $_POST and/or $_FILES i got this:

Log from the $_FILES

2016/07/07 18:18:12 [info] [application] FILE : 

Array
(
)

Log that I receive from the $_POST

[Puntointeres] => Array
    (
        [Name] => My Name
        [Description] => What ever
        ......
        [image] => 
    )

Any help?

3
  • Did you look at the generated html to make sure that the enctype attribute is actually set? given that your image input shows up in $_POST, either you're not building the form correctly, or something's mangling it. Commented Jul 7, 2016 at 16:27
  • The input field should be $form->field($model, 'image')->fileInput() Commented Jul 7, 2016 at 16:28
  • Hi, I'm using Yii 1.1 so I dont have the function $form->field($model, 'image')->fileInput(); Commented Jul 8, 2016 at 7:26

3 Answers 3

3

Probably you forgot to set the enctype:

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

Also your field is incorrect, it should be:

$form->field($model, 'image')->fileInput()

Also remember Yii does not send the value of the image in $_POST. Through $_POST it sends the hidden field it generates while using inputFile.

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

1 Comment

Hi, I'm using Yii 1.1 so I dont have the function $form->field($model, 'image')->fileInput(); Also I have the 'enctype' => 'multipart/form-data' option in my form.
1

The problem was related with the SubmitButtonAjax that was serializing the fields and the file field was converted to a string.

Comments

0

Hi Did you check your html generated in Developer tool? I has enctype or not . If not then add this in your form options.

'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),

Also you have to call CUploadedFile to get uploaded file info

use this link to upload a file in yii.http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/

Info about CUploadedFile file http://www.yiiframework.com/doc/api/1.1/CUploadedFile

1 Comment

Hi, My form has the 'enctype' => 'multipart/form-data', I know I have to use the CUploadedFile but the problem is that the file doesnt arrive to the controller. Seems a form issue. As I tried with Postman to send the form info to my Controller and I saw in my low the $_FILES with the image.

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.