0

我的模型代码:

      public $image;

      return array(
        array('filename', 'required'),
        array('image', 'file', 'types'=>''),
        array('filename', 'length', 'max'=>11),
        array('id, filename', 'safe', 'on'=>'search'),
    );

我的视图代码:

     <?php echo CHtml::activeFileField($model, 'image'); ?>

我的控制器代码:

     $model =  new TblUpload;
     $model->attributes=$_POST['TblUpload'];
     $img = CUploadedFile::getInstance($model,'image');
     if($img->saveAs(Yii::app()->basePath.'/../images/'.$img))
            {       
                $model->filename = $img;
                $model->save(false);
            }
     }
    $this->render('uploadfile',array('model'=>$model));
}

嗨,使用此代码的朋友,我可以上传所有类型的文件,例如图像和文档,但我无法上传视频....我也更改了我的 php.ini 文件 max_upload_size...

4

2 回答 2

0

最后我整理了如何上传图片......这是我的控制器代码......

公共函数 actionUploadfile()

{
        $model =  new TblUpload;    
        if(isset($_POST['TblUpload']))
        {           
            $model->image=CUploadedFile::getInstance($model,'image');
            if($model->save())
            {
            $model->image->saveAs('/wamp/www/fileupload/images/'.$fileName);
            }*/
            $model->attributes=$_POST['TblUpload'];
            $img = CUploadedFile::getInstance($model,'image');
            if($img->saveAs(Yii::app()->basePath.'/../images/'.$img))
            {

                $model->filename = $img;
                $model->save(false);
            }   
        }
    $this->render('uploadfile',array('model'=>$model));
}

这是我弄错的地方......但现在我正在使用uploadify扩展来上传各种文件并对其进行编辑以一次用于多次上传

于 2012-12-04T06:44:59.693 回答
0

In your form view, have you set the enctype for the form?

$form = $this->beginWidget(
    'CActiveForm', array(
        'id' => 'my-form',
        'htmlOptions' => array(
            'enctype' => 'multipart/form-data'
        ),
    )
);

It could also possibly be the path to the folder you are trying to upload to doesn't exists or is incorrect. Try using getcwd(), eg:

$model->my_image->saveAs(getcwd()."/uploads/myfile.jpg");

This is most likely the problem, your path in your code above is:

 if($img->saveAs(Yii::app()->basePath.'/../images/'.$img))

The basepath and followed by the ../ probably wrong unless you areally are trying to upload in the directory before your root?

于 2012-11-29T11:05:28.263 回答