Upload file without model


(Faisal Irshad) #1

Hi all,

I am new to yii, and unfortunately stuck on file handling. i want to upload files, a form has array of filefields and unable to get its value in controller.

for example in form there is array of filefield. e.g

echo CHtml::fileField(‘docFile[]’,’’);

echo CHtml::fileField(‘docFile[]’,’’);

echo CHtml::fileField(‘docFile[]’,’’);

(let say three times, docFile is not related to any model)

the form has ‘htmlOptions’ => array(‘enctype’ => ‘multipart/form-data’

Please let me know what code will be to upload and update database in controller file.

Here is my code

            $i=0;


	foreach ($_POST['docType'] as $cp ) {


		$c_document = new CaseDocument;


		


		$c_document->docTitle 	= $_POST['docName'][$i];


		


		$rnd = rand(0,9999);


		$uploadedFile=CUploadedFile::getInstanceByName($_POST['docFile'][$i]);


		


                    $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name


                    $c_document->fileName = $fileName;	


		$c_document->save();


		


		$uploadedFile->saveAs(Yii::app()->basePath.'/../uploads/documents/'.$fileName);  


		$i++;


	}

i am also getting erro ‘undefine index’ for $_POST[‘docFile’][$i]. please help me with complete example code of _form and controller actionCreate method.

Thanks