mahsa.teimourikia
mahsa.teimourikia

Reputation: 1774

Yii framework, uploading files doesn't work

I have a form and I want to upload a file. here is my code:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
   'id'=>'show-form',
   'enableAjaxValidation'=>false,
       'htmlOptions' => array('enctype' => 'multipart/form-data'),

)); ?>

<fieldset>
    <legend>DATI TECNICI</legend>

        <div class="row">
           <?php echo $form->labelEx($model,'tec_data_file'); ?>
           <?php echo $form->fileField($model,'tec_data_file',array('size'=>45,'maxlength'=>45)); ?>
           <?php echo $form->error($model,'tec_data_file'); ?>
        </div>
</fieldset>
<?php $this->endWidget(); ?>

</div><!-- form -->

There was nothing added to the database after submitting, I did a bit of debuging with firebug and figured out that filefield generates a code like this:

<input id="ytShow_tec_data_file" type="hidden" name="Show[tec_data_file]" value="">
<input id="Show_tec_data_file" type="file" name="Show[tec_data_file]" maxlength="45" size="45">

and two data are sent by $_POST for tec_data_file (which is my file field in db). The first var is empty (I think it is related to first hidden input). and the second one contains my file. and when I'm assigning the variables to my model for saving:

$modelPhoto->attributes = $_POST['Photo'];

the tec_data_file gets an empty string! So nothing gets uploaded to my db. Anyone have an idea how to solve this? If you need more i

Upvotes: 2

Views: 3614

Answers (1)

Dan Blows
Dan Blows

Reputation: 21174

You need something like:

$model = new Photo;
$model->attributes = $_POST['Photo'];
$model->image = CUploadedFile::getInstance($model,'file');

where file is the name of the field.

I haven't tested this, it's from the documentation.

Upvotes: 2

Related Questions