Saving image use CUpLoadedFile

hello all…

Thanks before to read my post… i need anything help about saving image.

I did my code below by reading from :

http://www.yiiframework.com/forum/index.php?/topic/3457-issue-with-uploading-to-database/

http://www.yiiframework.com/doc/cookbook/2/

if i have got miss understanding , i wish anyone tell me too…

I have a task to upload an image…

what i have done, i will tell below :

My Model:

Image.php

public function rules()

{

return array(

array('', 'safe', 'on'=>'search'),


      array('gambar','file',


            'types'=>'jpg, gif, png, jpeg',


            'maxSize'=>1024 * 1024 * 50,


            'tooLarge'=>'The file was larger than 50MB. Please upload a smaller file.',


            'allowEmpty'=>true,


            ),


);

}

My Controller : ImageController.php

public function actionCreate()

{

	$model=new Image;


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


	{


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


                    //$model->gambar = 'E:\Windows_Data\titi\Ling2\centil.jpg';


                    $img=CUploadedFile::getInstance($model,'gambar');


                        


                    if($img === null)


                        echo 'KOSONG TERNYATA'; // Run this Statement ( $img is null )


                    if(!$img->getHasError())


                        $model->Path = file_get_contents($img->getTempName());


		


                    if($model->save())


                    {


                        echo 'Name : '.$img->name;


                        echo 'Type : '.$img->type;


                        echo 'Size : '.$img->size;


                        echo 'Temp Name : '. $img->tempName;


                        //$model->Path->saveAs($model->Path->name);


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


                    }


	}





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


		'model'=>$model,


	));


}

My View : _form.php

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

    //'enctype'=&gt;'multipart/form-data',


'id'=&gt;'image-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&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,'Title'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Title',array('size'=&gt;60,'maxlength'=&gt;200)); ?&gt;


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


&lt;/div&gt;





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


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Path',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


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


&lt;/div&gt;





    &lt;?php //echo CHtml::form('','post',array('enctype'=&gt;'multipart/form-data')); ?&gt;


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


    &lt;?php //echo CHtml::endForm(); ?&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(); ?>

I very stuck with all of my experiment… can anyone help me to give me a clue or anything , to solve my problem here… I GOT $img STILL NULLL.

What I want to do is : i want to try to save an image into database with column type BLOB ( if there is another better way than what i did here, you can give me an alternatif too )…

help me plz… thanks before for all…

Thanks a lot

You need to uncomment the enctype declaration in your _form file.

Switch:




//'enctype'=>'multipart/form-data',



To:




'enctype'=>'multipart/form-data',



Yes thanks blindMoe… this worked :rolleyes: