File Upload Form

I’m trying to create a file upload form but I cannot get this to work. I want the uploaded file to be saved in folder ‘protected/data/’.


<?php echo CHtml::beginForm(); ?> 

<?php echo CHtml::label('Attach Report', 'Application_word_report'); ?> 

<?php echo CHtml::fileField('Application[word_report]', '', array('id'=>'Application_word_report')); ?> 

<?php echo CHtml::submitButton('Submit'); ?> 

<?php echo CHtml::endForm(); ?> 




I get error message: Non-static method CUploadedFile::saveAs() should not be called statically, assuming $this from incompatible context

Do I need to create the form tag in specific way, eg. enctype="multipart/form-data. There are no examples of how to do this in the documentation.

try this

and yes, you need to setup your form like this

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

Cheers mbi. In your example the only thing different is that you used a Model for your form. I can’t figure out why my code does not seem to work.

Why are you using saveAs() as a static method? (

$file = CUploadedFile::getInstanceByName('Application[word_report]');

$file->saveAs('protected/data/'.$file->name); // Seems you forgot to specify the filename.

And ofcourse enctype="multipart/form-data" should be specified.

Cheers andy_s. I now do not get any errors, but the file is not being saved to the folder. Any idea?

Check the folder’s permissions (must be writable) and try to save file as Yii::app()->basePath.’/data/’.$file->name. Here I assume the basePath is set to default value (protected directory).

Hi I think something else seems to be wrong. I put in a die() in my controller:





But it does not die.

This problem only happens after I put the enctype="multipart/form-data" on my form.

Maybe because $_POST[‘Application’][‘word_report’] is not set? :)

Uploaded files are stored in the $_FILES array.

if(isset($_FILES[‘Application’])) works ;)

Is it possible to force a "save as" dialog when clicking on a file URL?

Anyone able to advise regarding my last query? Thanks.

Anyone able to advise regarding my last query? Thanks.

Hi all…I really need some help, as nothing on the net refers to my specific situation.

I am using the wizard-behavior extension. It is built using CFormModel rather than CActiveRecord Model. I am pretty much stuck using things the way they are setup…so…

I have a 4 step registration form, first 3 steps are fine, however, the 4th page is to upload a photo. I can not for the life of me get it to pass the file to the controller.

Here is my Model:


class UploadPhoto extends CFormModel {

	public $userPhoto;

	public function rules() {

		return array(




	public function attributeLabels() {

		return array(

			'userPhoto'=>'Upload A Photo',




	public function getForm() {

		return new CForm(array(






					'hint'=>'Please Choose One',






					'label'=>'Reset Form'







		), $this);



	 * Recovers a partially completed registration.

	 * @param string User registration UUID; data will be recovered from the registration file.

	 * @return mixed array: user registration data; boolean: false if the registration data could not be recovered


	public function recoverRegistration($uuid) {

		$registrationFile = Yii::getPathOfAlias('application.runtime.registration').DIRECTORY_SEPARATOR.$uuid.'_draft.txt';

		if (file_exists($registrationFile)) {

			$data = unserialize(@file_get_contents($registrationFile));


			return $data;



			return false;



	 * Saves a draft of registration data.


	public function saveRegistration($data) {

		$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

			mt_rand(0, 0xffff), mt_rand(0, 0xffff),

			mt_rand(0, 0xffff),

			mt_rand(0, 0x0fff) | 0x4000,

			mt_rand(0, 0x3fff) | 0x8000,

			mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)


		$registrationDir = Yii::getPathOfAlias('application.runtime.registration');

		$registrationDirReady = true;

		if (!file_exists($registrationDir)) {

			if (!mkdir($registrationDir) || !chmod($registrationDir, 0775))

				$registrationDirReady = false;


		if ($registrationDirReady && file_put_contents(

			$registrationDir.DIRECTORY_SEPARATOR.$uuid.'_draft.txt', serialize($data)


			return $uuid;

		return false;



And My Form:


echo $event->sender->menu->run();

echo '<div>Step '.$event->sender->currentStep.' of '.$event->sender->stepCount;

echo '<h3>'.$event->sender->getStepLabel($event->step).'</h3>';

echo CHtml::tag('div',array('class'=>'form'),$form);

And my Controller:

	public function wizardFinished($event) {

        $command = Yii::app()->db->createCommand();


			$db_array = merge_array($event->data['User'],$event->data['ContactDetails'],$event->data['UserEmployeeProfile'],$event->data['UploadPhoto']);	


			$db_array = array_merge($event->data['User'],$event->data['ContactDetails'],$event->data['UserEmployerProfile'],$event->data['UploadPhoto']);







		if ($event->step===true){




            // ...redirect to another page

			$this->render('completed', compact('event'));


			$this->render('finished', compact('event'));





	public function UploadPhoto($inPhoto){



		// receive file from post

		//$Upload = new Upload( (isset($inPhoto) ? $inPhoto : null) );

		$fullpath = 'C:/Users/Pirificio/Pictures/'.$inPhoto; //Used to test locally


		$Upload = new Upload($fullpath);

		$Upload->jpeg_quality  = 100;

		$Upload->no_script     = false;

		$Upload->image_resize  = true;

		$Upload->image_x       = 700;

		$Upload->image_y       = 500;

		$Upload->image_ratio   = true;


		// some vars

		$newName  = md5($inPhoto);

		$destPath = Yii::app()->getBasePath().'/../images/members/';

		$destName = 'big';

		// verify if was uploaded

		if ($Upload->uploaded) {

    		$Upload->file_new_name_body = $newName;                     



        // if was processed

        	if ($Upload->processed) {

            	$destName = $Upload->file_dst_name;    


            	// create the thumb  



            	$destName = 'small';

            	$Upload = new Upload($destPath.$destName);

            	$Upload->file_new_name_body   = $newName;

            	$Upload->no_script            = false;

            	$Upload->image_resize         = true;

            	$Upload->image_x              = 120;

            	$Upload->image_y              = 80;

            	$Upload->image_ratio          = true;



        	} else {








As you can see, I am kinda cheating a bit, and basically processing all info collected from the wizard in $event->data and mashing it into an array and writing it to the db. Before I write to the db though, I need to get the file, and process the photo. Basically the file isn’t passed to $event->data like everything else is.

HELP Please!!!