How do I include an image upload field in standard form?

I've got a form and all fields are working correctly but I can't get the image upload to work.  There doesn't seem to be any complete documentation in this regard. I'm new to Yii and MVC programming in general. Can someone please assist?  What do you need to know about my current form setup?

With Yii 1.0.2, here's what you need to do in order to upload a file:

  1. Assuming you already have a form model (or an active record class), you should create an attribute to store the uploaded file name. Let's same it is named as 'file'. You normally should declare a validation rule for this attribute:


<?php


class MyFormModel extends CFormModel


{


	public $file;





	public function rules()


	{


		return array(


			array('file', 'file', 'types'=>'gif, png, jpg, jpeg, bmp', 'maxSize'=>2097152),


		);


	}


	


	....


}


  1. In your view, you should have something like the following to render the file upload field:


<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>


....


<?php echo CHtml::activeFileField($model,'file'); ?>


....


</form>


  1. In your controller action, you may use the following code :


<?php


$model=new MyFormModel;


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


{


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


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


	if($model->validate())


	{


		// save the file


		$model->file->saveAs('localFileName');


		// ....


	}


}


$this->render('viewName',array('model'=>$model));


Thanks for your assistance.

I have implemented your suggestions in the following code:

MODEL

class PerformJoinForm extends CFormModel

{

public $stagename;


public $realname;


public $email;


public $picture;

        public $file;

        public $file_name;

public $profile;</span>


public $ageid;


public $hispeed;


public $hirescam;


public $verifyCode;





/**


 * Declares the validation rules.


 */


public function rules()


{


	return array(


		// name, email and profile are required


		array(&#039;realname, email, profile, ageid, hispeed, hirescam&#039;, &#039;required&#039;),


		// email has to be a valid email address


		array(&#039;email&#039;, &#039;email&#039;),


		<span style='color: red'>array(&#039;file_name&#039;, &#039;file&#039;, &#039;types&#039;=&gt;&#039;png, jpg&#039;, &#039;maxSize&#039;=&gt;100000),</span>


		// verifyCode needs to be entered correctly


		array(&#039;verifyCode&#039;, &#039;captcha&#039;, &#039;allowEmpty&#039;=&gt;!extension_loaded(&#039;gd&#039;)),


	);


}

VIEW

  <?php echo CHtml::form(’’, ‘post’, array(‘enctype’ => ‘multipart/form-data’)); ?>

  <div class="simple">

      <?php echo CHtml::activeLabel($performjoin,'stage name'); ?>

      <?php echo CHtml::activeTextField($performjoin,'stagename'); ?>

  </div>

  <div class="simple">

      <?php echo CHtml::activeLabel($performjoin,'real name'); ?>

      <?php echo CHtml::activeTextField($performjoin,'realname'); ?>

  </div>

  <div class="simple">

      <?php echo CHtml::activeLabel($performjoin,'email'); ?>

      <?php echo CHtml::activeTextField($performjoin,'email'); ?>

  </div>

  <div class="simple">

      <?php echo CHtml::activeLabel($performjoin,'picture'); ?>

      <?php echo CHtml::activeFileField($performjoin,‘file’); ?>

  </div>

CONTROLLER

public function actionPerformJoin()


{


	$performjoin=new PerformJoinForm;

           

	if(isset($_POST&#91;&#039;PerformJoinForm&#039;]))


	{


	$performjoin-&gt;attributes=$_POST&#91;&#039;PerformJoinForm&#039;];

                $performjoin->file=CUploadedFile::getInstance($performjoin,‘file’);

       

		if($performjoin-&gt;validate())


		{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  

            // save the picture file

&nbsp; &nbsp; <span style='color: red'>$performjoin-&gt;file-&gt;saveAs(&#039;new_performers/testmodel.jpg&#039;);</span>

           

            // prepare the email

            $headers="From: {$performjoin->email}\r\nReply-To: {$performjoin->email}";

            $subject="Another WTC Performer Application";

            $body="Stage Name: $performjoin->stagename\r\nReal Name: $performjoin->realname\r\nEmail: $performjoin->email\r\nPicture: $performjoin->file\r\nProfile: $performjoin->profile\r\nAge Documentation: $performjoin->ageid\r\nHi-Speed Connection: $performjoin->hispeed\r\nHi-Res Camera: $performjoin->hirescam\r\n";

           

			mail(Yii::app()-&gt;params&#91;&#039;adminEmail&#039;],$subject,$body,$headers);


			Yii::app()-&gt;user-&gt;setFlash(&#039;performjoin&#039;,&#039;&lt;br /&gt;&lt;br /&gt;&lt;center&gt;Thank you for sending us your application. We will respond as soon as possible.&lt;/center&gt;&#039;);


			$this-&gt;refresh();


		}


	}


	$this-&gt;render(&#039;performjoin&#039;,array(&#039;performjoin&#039;=&gt;$performjoin));


}

When I go to test the form I am getting a validation error:

Please fix the following input errors:

File Name cannot be blank.

Can you spot anything I am doing wrong?

I see two places where you have 'file' as the argument.  I think it should be changed to 'file_name' as you have it declared with that name in your model.

Sorry to be so naive.

I changed

$performjoin->file->saveAs('new_performers/testmodel.jpg');

to

$performjoin->file_name->saveAs('new_performers/testmodel.jpg');

and

<?php echo CHtml::activeFileField($performjoin,'file'); ?>

to

<?php echo CHtml::activeFileField($performjoin,'file_name'); ?>

Now I am getting the following:

Fatal error: Call to a member function saveAs() on a non-object in /home/wildcams/public_html/protected/controllers/SiteController.php on line 66

Thanks for your assistance.

Don't change the 'file' object, I did not say that.  I used the word "argument".  The arguments that you have set to 'file' may need to be changed to 'file_name'.

ex:

$a->foo('bar');

'bar' is an argument, 'foo', is a method, and $a is a var holding a object (that has the 'foo' method).

<?php echo CHtml::activeFileField($performjoin,'file'); ?>


to


<?php echo CHtml::activeFileField($performjoin,'file_name'); ?>

is a step forward

$performjoin->file->saveAs('new_performers/testmodel.jpg');


to


$performjoin->file_name->saveAs('new_performers/testmodel.jpg');

is a step backwards.

Great! Got it. it's been a long day.

Sorry, one last thing.  How do I concat my file path to the uploaded file name for the following line:

$performjoin->file->saveAs('new_performers/$file_name');

$performjoin->file->name