Good afternoon all, I think I am real close just missing something small I am sure. Below is the code in my form and the action from my controller.
Both files successfully upload but only the last one gets posted to the database, I am sure its an overwriting issue in the loop but maybe one of y’all smart people could help. I need it to add both files to the database too along with uploading them.
Thanks,
public function actionForm()
{
$model=new Userfiles;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Userfiles']))
{
$model->attributes=$_POST['Userfiles'];
$sFiles = CUploadedFile::getInstancesByName('filename');
if(isset($sFiles) && count($sFiles)> 0)
{
foreach ($sFiles as $key=>$sFile)
{
$url = Yii::getPathOfAlias('webroot').'/images/';
$i++;
if($model->save()){
$model->filename = $sFile->name;
$date = new DateTime();
$model->origfile=$model->filename;
$new_file = $date->getTimestamp().$model->filename;
$model->filename = $new_file;
$sFile->saveAs($url.$new_file,0777);
$model->save();
}
}
//model->filename->saveAs(Yii::app()->baseUrl.'/images');
// redirect to success page
//$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('_form2',array(
'model'=>$model
));
}
<?php
/* @var $this UserfilesController */
/* @var $model Userfiles */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'userfiles-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data')
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php $this->widget('CMultiFileUpload', array(
'name' => 'filename',
'accept' => 'jpeg|jpg|gif|png',
'duplicate' => 'File is not existed!',
'denied' => 'Not images', // useful,
'htmlOptions'=>array(
'class'=>'form-control',
'multiple'=>'multiple'
),
)); ?>
<?php echo $form->labelEx($model,'filename'); ?>
<?php //echo CHtml::activeFileField($model, 'filename[]',array('class'=>'myClass','style'=>'display:block;','multiple'=>true)); ?>
<?php /*echo CHtml::link(CHtml::image(yii::app()->baseURL.'/images/profile.png','',array('width'=>200,'height'=>80)),'#',array('onclick'=>'$("#Userfiles_filename").click();'));*/ ?>
<?php //echo $form->textField($model,'filename',array('size'=>60,'maxlength'=>77)); ?>
<?php echo $form->error($model,'filename'); ?>
</div>
<!-- <div class="row">
<?php //echo $form->labelEx($model,'description'); ?>
<?php //echo $form->textArea($model,'description',array('rows'=>5,'cols'=>50)); ?>
<?php //echo $form->error($model,'description'); ?>
</div>
-->
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'user_id'); ?>
<?php echo $form->dropDownList($model, 'user_id', CHtml::listData(User::model()->findAll(), 'id', 'username'),array('prompt' => 'Select a User')); ?>
<?php echo $form->error($model,'user_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->