Yii2:上传多个文件时,在null上调用成员函数saveAs()

时间:2016-02-10 14:37:37

标签: php yii2

上传多个文件时收到此错误:

当我将[['file'], 'file', 'maxFiles' => 4],置于模型中时出现以下错误:

Call to a member function saveAs() on null

但是,当我将此[['file'], 'file'],放入模型中时,会上传。

为什么我会收到错误?

查看:

<?php  echo $form->field($model,'file[]')->label(false)->widget(FileInput::classname(), 
                          [
                          'options'=>['accept'=>'image/*', 'multiple'=>true],
                          'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
                          ]]);  
                      ?> 

控制器:

public function actionCreate()
    {
        $model = new RoomTypes();

        if ($model->load(Yii::$app->request->post()))
        {
            $imageName = $model->room_type;
            $model->file = UploadedFile::getInstance($model, 'file');
            $model->file->saveAs( 'uploads/room_img/'.$imageName.'.'.$model->file->extension);
            //save the path in the db column
            $model->images = 'uploads/room_img/'.$imageName.'.'.$model->file->extension;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } 
        else 
        {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

2 个答案:

答案 0 :(得分:1)

根据各自的文档使用getInstances而不是getInstance,第一个返回给定模型属性的所有上传文件,而第二个返回单个文件属性。 然后循环并逐个保存:

if ($model->load(Yii::$app->request->post())) { 
     $imageName = $model->room_type;

     $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');

     $all_files_paths = [];

     foreach ($model->imageFiles as $file_instance) {
        // this should hold the new path to which your file will be saved
        $path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension;

        // saveAs() method will simply copy the file 
        // from its temporary folder (C:\xampp\tmp\php29C.tmp) 
        // to the new one ($path) then will delete the Temp File
        $file_instance->saveAs($path);

        // here the file should already exist where specified within $path and 
        // deleted from C:\xampp\tmp\ just save $path content somewhere or in case you need $model to be
        // saved first to have a valid Primary Key to maybe use it to assign
        // related models then just hold the $path content in an array or equivalent :
        $all_files_pathes []= $path;
     }

     $model->save();

     /*
        after $model is saved there should be a valid $model->id or $model->primaryKey
        you can do here more stuffs like :

        foreach($all_files_pathes as $path) {
            $image = new Image();
            $image->room_id = $model->id;
            $image->path = $path;
            $image->save();
        }
     */

     return $this->redirect(['view', 'id' => $model->id]);
 }

有关详细信息,请参阅docs

答案 1 :(得分:0)

这也许对我有用。

UploadedFile :: getInstance返回文件数组。使用getInstanceByName或UploadedFile :: getInstance($ model,'file')[0]。