A
A
Alexander Borisovich2012-08-02 16:28:29
Yii
Alexander Borisovich, 2012-08-02 16:28:29

Image transmission check problem. Glitch YII?

Version: 1.1.10

A simple photo fill form. (_form.php)

View:

 <div class="row">
  <?php echo $form->labelEx($model,'foto'); ?>
   <?php  echo $form->fileField($model,'foto',array()); ?>
  <?php echo $form->error($model,'foto'); ?>
 </div>

In the 'foto' model, this is a string

Select an image and try to save it.
This is not how it works:

In the controller, the actionUpdate function


if ( $model->foto!='')
   {
   
   $fname='123.jpg';
   $img=CUploadedFile::getInstance($model,'foto'); 
   $model->foto=$fname;
   
   }


This is how it works:

if ($_FILES['Pilgrims']['name']['foto'])
   {
   
   $fname='123.jpg';
   $img=CUploadedFile::getInstance($model,'foto'); 
   $model->foto=$fname;
   
   }


How is it that $model->foto is empty, but then the file is safely saved from it via CUploadedFile? When dumping $model->foto is ' '

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sdevalex, 2012-08-02
@sdevalex

The field fotomust be virtual:

class Item extends CActiveRecord
{
  public $foto;
}

$model->foto = CUploadedFile::getInstance($model,'foto'); 
$model->photoFile = '123.jpg';

And in the model:
public function afterSave()
{
$this->foto->saveAs('/path/to/upload/folder/'. $this->photoFile);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question