Yii Framework Forum: Upload file without model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Upload file without model Manage file field Rate Topic: -----

#1 User is offline   faisi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-May 17

  Posted 20 May 2017 - 03:31 PM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users