5

I am simply trying to upload a file using a model. I get the 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 an another Exception:

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

What is 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));
    }
}

VIEW: 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();

 ?>

3 Answers 3

6

CFormModel doesn't have a method named save(), if you want to call it you have to implement it, but what you want in your case is to use the validate method

And if MyFile doesn't have a related db table then it shouldn't extend CActiveRecord.

You can validate that the uploaded image is a gif, a png or a jpg by calling 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));
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Now I have a table and I am using CActiveRecord. Now I have an another Exception: Missing the temporary folder to store the uploaded file "xyz.jpg".
Hum the folder Yii::app()->runtimePath.'/temp/uploadDirectory/' exists? if not then you should create it before saving the image with something like mkdir($yourPath, 0775, true) (true is to recursively create the folders)
yes, the directory exists, the same error message.. I think, I must set the temporary directory somewhere in the setting, but I currently do not know, where?
I've found it: On the test-server there was no entry in the php.ini file at "upload_tmp_dir". I've changed it to /var/tmp
0
  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));
}

Comments

0
   $img = CUploadedFile::getInstance($model,'file');
        $randomNAme = hash('sha512',$model->file);

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

1 Comment

use this to store the file uploaded using Yii 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.