When I update a record, i.e a Contact, the attachments (images) are overwritten. How do I just insert the the newly attached images and not get the old ones be overwritten? Please see my codes below (I am using Yii 2.0 PHP framework).
Controller
public function actionUpdate($id)
{
$session = Yii::$app->session;
$model = $this->findModel($id);
$cur_businessname = $model->business_name;
$attachment = $model->attachment;
$files = array();
if ($model->load(Yii::$app->request->post())) {
$model->attachment = UploadedFile::getInstances($model, 'attachment');
if($model->attachment) {
if (empty($model->attachment)) {
$model->attachment = UploadedFile::getInstance($model, 'attachment');
$path = 'assets/images/default.png';
$model->attachment = $path;
} else {
foreach ($model->attachment as $attachment) {
$path = 'archive/contact/'.$attachment->baseName.'.'.$attachment->extension;
$count = 0;
{
while(file_exists($path)) {
$path = 'archive/contact/'.$attachment->baseName.'_'.$count.'.'.$attachment->extension;
$count++;
}
}
$attachment->saveAs($path);
$files[] = $path;
}
$model->attachment = implode(',', $files);
}
} else {
$model->attachment = $attachment;
}
//$updatedAttachments = array_push($attachment,$path);
//var_dump($attachment);
//exit;
$model->save();
if($model->save()) {
Yii::$app->session->setFlash('success', "Success!");
} else {
Yii::$app->session->setFlash('error', "Contact Creation Failed!");
}
$model->refresh();
return $this->redirect(['index', 'id' => $session['user_id']]);
} else {
return $this->renderAjax('update', [
'model' => $model,
]);
}
}
View
<?php
if (!empty($model->attachment)) {
$model->attachment = explode(',', $model->attachment);
$file = '';
foreach ($model->attachment as $attachment) {
$file.= Html::img(Yii::$app->urlManager->baseUrl . '/'. $attachment, ['class'=>'file-preview-image', 'alt'=>'Attachment', 'title'=>'Attachment']).',';
}
echo FileInput::widget([
'model' => $model,
'attribute' => 'attachment[]',
'options' => ['multiple' => true],
'pluginOptions' => [
'initialPreview'=>[
[$file,]
],
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
'browseClass' => 'btn btn-primary btn-block',
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
'browseLabel' => 'Attach Business Card',
'maxFileCount' => 2,
'allowedFileExtensions'=>['jpg','gif','png'],
'overwriteInitial' => true,
'uploadAsync' => true,
],
'options' => ['accept' => 'image/*', 'multiple' => true]
]);
} else {
echo FileInput::widget([
'model' => $model,
'attribute' => 'imageUrl[]',
'options' => ['multiple' => true],
'pluginOptions' => [
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
'maxFileCount' => 2,
'browseClass' => 'btn btn-primary btn-block',
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
'browseLabel' => 'Attach Business Card',
'allowedFileExtensions'=>['jpg','gif','png'],
],
'options' => ['accept' => 'image/*', 'multiple' => true]
]);
}
?>
I have tried array_push
but it's not working. The $attachment
on the 6th line of actionUpdate
holds the old attachments. Now, I can't anymore use $attachment
inside if ($model->load(Yii::$app->request->post()))
since it's already after posting/submitting the form.
How do I just insert the newly attached files without overwriting the old ones? Your comments/answers will be of great help!