简体   繁体   中英

Yii file upload $_FILE is empty

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?

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.

问题与序列化字段的SubmitButtonAjax有关,文件字段已转换为字符串。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM