Yii uploads file using model

I am just trying to upload a file using a model. I get an exception message in the current situation (see Model / Controller / View below):

CException
MyFile and its behaviors do not have a method or closure named "save". 

If my model extends CActiveRecord instead of CFormModel, there is one more exception:

CDbException
The table "MyFile" for active record class "MyFile" cannot be found in the database. 

What's my mistake? These are the files:

MODEL: MyFile.php

class MyFile extends CFormModel {
    public $image;
    public function rules () {
        return array (
            array ('image', 'file', 'types' => 'gif, jpg, png'),
        );
    }
}

CONTROLLER: MyFileController.php

class MyFileController extends CController {
    public function actionCreate() {
        $model = new MyFile;

        if(isset($_POST['MyFile'])) {

            $model->attributes=$_POST['MyFile'];
            $model->image=CUploadedFile::getInstance($model,'image');
            if($model->save()) {
                $path = Yii::app()->runtimePath.'/temp/uploadDirectory/'.$model->image;
                $model->image->saveAs($path);
            }
        }
        $this->render('create', array('model'=>$model));
    }
}

BROWSE: create.php

 <h1>File-Upload</h1>

 <?php

    echo CHtml::form('','post',array('enctype'=>'multipart/form-data'));
    echo CHtml::activeFileField($model, 'image');
    echo CHtml::submitButton('abschicken', array('name' => 'submit'));
    echo CHtml::endForm();

 ?>
+4
source share
3 answers

CFormModeldoesn't have a method with a name save(), if you want to call it, you need to implement it, but in your case you have to use the methodvalidate

And if it MyFiledoes not have a related db table, it should not expand CActiveRecord.

, gif, png jpg, validate():

class MyFileController extends CController {
    public function actionCreate() {
        $model = new MyFile;

        if(isset($_POST['MyFile'])) {

            $model->attributes=$_POST['MyFile'];
            $model->image=CUploadedFile::getInstance($model,'image');
            if($model->validate()) {
                //The image is valid, you can save it
                $path = Yii::app()->runtimePath.'/temp/uploadDirectory/'.$model->image;
                $model->image->saveAs($path);
            }
            $this->render('create', array('model'=>$model));
        }
    }
}
+6
  public function actionCreate() {
    $model = new Item;
    if (isset($_POST['Item'])) {
        $model->attributes = $_POST['Item'];
        $model->images = CUploadedFile::getInstance($model, 'images');
        if ($model->save()) {
            $path = Yii::app()->basePath . '/../uploads/' . $model->images;
            $model->images->saveAs($path);
            // redirect to success page
        }
    }
    $this->render('upload', array('model' => $model));
}
0
   $img = CUploadedFile::getInstance($model,'file');
        $randomNAme = hash('sha512',$model->file);

        $path = Yii::app()->basePath.'/'.'uploads/'.$img;
        $img->saveAs($path);
0

All Articles