Note that there are some explanatory texts on larger screens.

plurals
  1. POYii uploading pictures
    primarykey
    data
    text
    <p>I'm a new to yii, so I was following this <a href="http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/#hh1" rel="nofollow">tutorial</a>.</p> <p>when I try to upload picture it reports empty field even if it is not.</p> <p>my _form view:</p> <pre><code>&lt;div class="form"&gt; &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array( 'id'=&gt;'picture-form', 'enableAjaxValidation'=&gt;false, 'htmlOptions' =&gt; array('enctype' =&gt; 'multipart/form-data'), )); ?&gt; ... &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'path_to'); ?&gt; &lt;?php echo $form-&gt;fileField($model,'path_to'); ?&gt; &lt;?php echo $form-&gt;error($model,'path_to'); ?&gt; &lt;/div&gt; ... &lt;?php $this-&gt;endWidget(); ?&gt; &lt;/div&gt;&lt;!-- form --&gt; </code></pre> <p>and here is my action method:</p> <pre><code>public function actionCreate() { $model=new Picture; if(isset($_POST['Picture'])) { $model-&gt;attributes=$_POST['Picture']; $model-&gt;picture=CUploadedFile::getInstance($model,'path_to'); if($model-&gt;save()){ $log-&gt;lwrite('in save'.$model-&gt;picture); $model-&gt;picture-&gt;saveAs(Yii::app()-&gt;basePath.'/../images/'.$model-&gt;picture); $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id)); $log-&gt;lclose(); } } $this-&gt;render('create',array('model'=&gt;$model,)); } </code></pre> <p>when I print_r($_FILES) everything what should be there is there when I print_r($_POST) the field 'path_to' is empty and the validator probably is picking that one.</p> <p>I'm probably missing something here and I'm clueless.</p> <p>update1: I've noticed yii is using hidden field with the same name as file input, than attributes are read from $_POST, which is causing the engine reading empty hidden field. I understand that the hidden field is there for update when user is not entering new picture. Can anyone advice what is the best way of doing picture upload?</p> <p>update2: model code: class Picture extends CActiveRecord {</p> <pre><code> public $picture; ... public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('path_to, page_id', 'required'), array('page_id', 'numerical', 'integerOnly'=&gt;true), array('alt_text', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, alt_text, path_to, page_id', 'safe', 'on'=&gt;'search'), //array('path_to', 'file','types'=&gt;'jpg, gif, png', 'allowEmpty'=&gt;true, 'on'=&gt;'update'), //array('path_to', 'length', 'max'=&gt;255, 'on'=&gt;'insert,update'), array('path_to', 'unsafe'), ); } ... </code></pre> <p>Best! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload