我只是想使用模型上传文件。我在当前情况下收到异常消息(请参阅下面的模型/控制器/视图):
CException
MyFile and its behaviors do not have a method or closure named "save".
如果我的模型扩展了 CActiveRecord 而不是 CFormModel,则会出现另一个异常:
CDbException
The table "MyFile" for active record class "MyFile" cannot be found in the database.
我的错误是什么?这些是文件:
型号:MyFile.php
class MyFile extends CFormModel {
public $image;
public function rules () {
return array (
array ('image', 'file', 'types' => 'gif, jpg, png'),
);
}
}
控制器: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));
}
}
查看: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();
?>