简体   繁体   English

如何在Yii2的两个不同路径上载两个文件?

[英]How to upload two files at two different paths in Yii2?

public function actionCreate()
{
        $model = new RoomTypes();
        if ($model->load(Yii::$app->request->post())) 
        {
            $imageName_1 = $model->room_type.'_'.'1';
            $model->pic_1 = UploadedFile::getInstance($model, 'pic_1');
            $model->pic_1->saveAs('uploads/room_pics/'.$imageName_1.'.'.$model->pic_1->extension);
            $model->pic_1 = 'uploads/room_pics/'.$imageName_1.'.'.$model->pic_1->extension;

            $imageName_2 = $model->room_type.'_'.'2';
            $model->pic_2 = UploadedFile::getInstance($model, 'pic_2');
            $model->pic_2->saveAs('uploads/room_pics/'.$imageName_2.'.'.$model->pic_2->extension);
            $model->pic_2 = 'uploads/room_pics/pic2/'.$imageName_2.'.'.$model->pic_2->extension;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else 
        {
            return $this->render('create', [
                'model' => $model,
            ]);
        }  
}

I'm not able to store 2nd image in folder, this code is storing 1st or 2nd image in folder, only one image is getting stored not all two, how to achieve this? 我无法将第二张图片存储在文件夹中,此代码将第一张图片或第二张图片存储在文件夹中,只有一个图片不是全部都存储了,如何实现呢?

Update: **Model - ** 更新: **型号-**

public function rules()
    {
        return [
            [[/*'room_number','total_people', 'room_type', 'total_count', 'extra_beds', 'pic_1', 'pic_2', 'status', 'rate', 'adults_count', 'child_count'*/], 'required'],
            [['room_number','total_people','total_count', 'extra_beds', 'rate', 'adults_count', 'child_count'], 'integer'],
            [['status'], 'string'],
            [['pic_1', 'pic_2'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
            [['room_type'], 'string', 'max' => 40],
            [['description'], 'string', 'max' => 300],
            [['pic_1', 'pic_2'], 'string', 'max' => 500]
        ];
    } 

Seem you have a wrong name in $model->pic_2 assignment I see /pic2/ in kmore 似乎您在$model->pic_2分配中使用了错误的名称,我在kmore中看到了/pic2/ pic2 /pic2/

$model->pic_2 = 'uploads/room_pics/pic2/'.$imageName_2.'.'.$model->pic_2->extension;

$model->pic_1 = 'uploads/room_pics/'.$imageName_1.'.'.$model->pic_1->extension;

In the below lines of code the pathname don't match for me 在下面的代码行中,路径名与我不匹配

In the second i see a pic2 for more 在第二个我看到更多的pic2

        $model->pic_1->saveAs('uploads/room_pics/'.$imageName_1.'.'.$model->pic_1->extension);
        $model->pic_1 = 'uploads/room_pics/'.$imageName_1.'.'.$model->pic_1->extension;

        $model->pic_2->saveAs('uploads/room_pics/'.$imageName_2.'.'.$model->pic_2->extension);
        $model->pic_2 = 'uploads/room_pics/pic2/'.$imageName_2.'.'.$model->pic_2->extension;

In the two section or don't match in saveAs or don't match in assignement and when yuo retreive the path you don't found the picture file 在两部分中,或者在saveAs中不匹配或在分配中不匹配,并且当您回溯路径时,您找不到图片文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM