File uploading

Hi!

I’m developing a web application trough which users must be able to upload images and see uploaded images.

Here is a code of my mode class:

==============================================================================================

public function rules()

{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('etat', 'length', 'max'=>1),


		//array('image', 'length', 'max'=>1024),


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


		array('tbl_utilisateur_id, tbl_equipement_id', 'length', 'max'=>20),


		array('date_maj', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('date_maj, etat, image, id, tbl_utilisateur_id, tbl_equipement_id', 'safe', 'on'=>'search'),


	);


}

=============================================================================================================

in my controller I have :

=============================================================================================================

public function actionCreate()

{


	$model=new EtatEquipement;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





	if(isset($_POST['EtatEquipement']))


	{


		$model->attributes = $_POST['EtatEquipement'];                        


                    


                    $picture_file = CUploadedFile::getInstanceByName('imge');


                    // has the user uploaded a new file?


                    if($picture_file){                        


                        $picture_name = $picture_file->name;


                        $picture_file->SaveAs(Yii::app()->getBasePath().'\images\gallery' . $picture_name);


                    }else echo 'no picture file';


                    


                    //save the model


                    if($model->save()){





                        // redirect to success page


                        $this->redirect(array('view','id'=>$model->id));


                    }else{


                        echo 'not saved';


                    }


               }


                    $this->render('create',array(


                        'model'=>$model,


                    ));


}

=============================================================================================================

And in my view I have

=============================================================================================================

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'etat-equipement-form',


'enableAjaxValidation'=&gt;false,


    'htmlOptions'=&gt;array('enctype'=&gt;'multipart/form-data'),

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'etat'); ?&gt;


            &lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'etat', array(&quot;F&quot;=&gt;&quot;Fonctionnel&quot;, &quot;D&quot;=&gt;&quot;Défectueux&quot;, &quot;R&quot;=&gt;&quot;Rébuté&quot;)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'etat'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'image'); ?&gt;


	&lt;?php /* echo &#036;form-&gt;fileField(&#036;model,'image'); */ ?&gt;


            &lt;?php echo CHtml::activeFileField(&#036;model, 'image'); ?&gt;&lt;br&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'image'); ?&gt;   


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

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

=============================================================================================================

When I execute the actionCreate(), the text ‘no picture file’ is displayed.

Please could anyone have/know a way to upload an image in Yii aaplication?

Thanks for your help.

First of All, Search before you post.

Secondly, I think error is here


$picture_file = CUploadedFile::getInstanceByName('imge');

Should be


$picture_file = CUploadedFile::getInstanceByName('image');

And


$picture_file->SaveAs(Yii::app()->getBasePath().'\images\gallery' . $picture_name);

It should be




$picture_file->SaveAs(Yii::app()->getBasePath().'/images/gallery/' . $picture_name);



Thanks

Thanks PeRoChAk!

I have apply but, i have the same message displayed ‘no picture file’

thanks for your help!

Change


$picture_file = CUploadedFile::getInstanceByName('imge');

with


     $picture_file = CUploadedFile::getInstance($model, 'image');

Thanks PeRoChAk!

Now I have another PHP Error :


 move_uploaded_file(C:\server\www\myserver.dev\public_html\gimto.client\protected/images/gallery/java.jpg) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: No such file or directory

Caused by :




      // has the user uploaded a new file?

      if($model->image){

            $picture_file->SaveAs(Yii::app()->getBasePath().'/images/gallery/' . $model->image->name);

      }else echo 'no picture file';                



I’m working on Win7 OS.

Thanks for your help!

What I do is


Yii::app()->request->baseUrl.'./images/prix/'.$image->name

so, for your case




 $picture_file->SaveAs(Yii::app()->getBasePath().'./images/gallery/' . $model->image->name);



Try with:


$picture_file->SaveAs(Yii::getPathOfAlias('webroot').'/images/'.$picture_file->getName())

Thanks for your posts, it works very well now.