dongtiao2105 2015-10-02 08:31
浏览 36

在更新记录中插入而不是覆盖新上传

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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角
    • ¥15 webots有问题,无响应
    • ¥15 数据量少可以用MK趋势分析吗
    • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
    • ¥15 大智慧怎么编写一个选股程序
    • ¥100 python 调用 cgps 命令获取 实时位置信息
    • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
    • ¥15 C语言使用vscode编码错误
    • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
    • ¥20 ensp怎么配置让PC1和PC2通讯上