CUPloadedFile use

[size="3"]HI Everyone

I am trying to upload an image & have written the model & view also.

I am trying to write the controller & included the function as given below.

if (isset($POST[‘Career’])){

            	$model->attributes = $POST['Career'];

            	$model->uploaded_image =  CUploadedFile::getInstance($model, 'uploaded_image');




Image is uploading but is not saving. Can anybody help me in this?[/size]

You’re saving it empty (empty filename).

I recommend this wiki article for clearest explanation of the most simple use case. Here’s another wiki article which is less clear, but shows a more typical use case.

Thanks for your help Boaz. I have tried the solution suggested by you. But still inspite of selecting the image, upload the image error message is coming on submit. Can you help me in this.

model rule:

array(‘uploaded_image’, ‘length’, ‘max’ => 255),

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




            	$Attr = "uploaded_image";


            	$form->labelEx($profileAddModel, $Attr);


            	<div class="controls">


                	echo CHtml::activeFileField($profileAddModel, $Attr, array('class' => 'span3','maxlength' => 255)); 

                	echo $form->error($profileAddModel, $Attr);





if (isset($POST[‘UserProfiles’])) {

            	$model->attributes = $POST['UserProfiles'];

            	$model->uploaded_image = CUploadedFile::getInstance($model, 'uploaded_image');

            	if ($model->save()) {




I haven’t carefully checked your code but when you use saveAs() you need to specify a filename, not a directory name.

For example, the following will save the uploaded file in a directory which is parallel to /protected (you need to create it, if it fits you, and don’t forget to make sure the web server can write to it):

saveAs(Yii::app()->basePath . "/../resources/my_module_name/" . $someFilename);

And I strongly recommend that you check the manual. In this case, this is the section to read.

Thanks a lot! Boaz

I am very thankful to you for your quick help.

I’ll try your suggestions & let you know if I succeed.

Hi Boaz

Thanks for your support. but, am still not able to upload the file. The error which am getting is


  • [size="3"]Uploaded Image cannot be blank.[/size]

I am getting this error inspite of uploading the image. I guess there is some problem with the model. The rules for model which am using are:

[size=“3”]array(‘uploaded_image’, ‘length’, ‘max’ => 255),

array(‘uploaded_image’,‘file’, ‘types’=>‘jpg, gif, png’),[/size]

& controller is:

[size=“3”]if (isset($POST[‘UserProfiles’])) {

            	$rnd = rand(0,9999);

            	$model->attributes = $POST['UserProfiles'];

            	$uploadedFile = CUploadedFile::getInstance($model, 'uploaded_image');

            	$fileName = "{$rnd}-{$uploadedFile}";

            	$model->uploaded_image = $fileName;

            	if ($model->save()) {



Am struggling a lot. Can you please help me in this?

Did you change the enctype of the form? See the last line in the example below.

<div class="form">

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

	'id' => 'your-form-id',

	'enableAjaxValidation' => false,

	'htmlOptions' => array('enctype' => 'multipart/form-data'),

)); ?>

Yes I changed the enctype of the form.

Form is submitting but image is not saving in the database.

Can’t say for sure. Remove the ‘length’ rule in the model and try again. Other than that, if it doesn’t help, try to go back to the basic, simplify things by making your code as simple as possible. I recommend sticking to a simple example as described in this wiki article. Also, debug along the way. Is $_FILES populated with the file? (which means it is being submitted), do the temporary file exists? (probably on /tmp, with temp name noted in $_FILES), etc. Check step by step slowly until you get to the step in which the system fails and doesn’t behave as expected.