1

I have a problem with Yii:

I have a Model called Slider.php and a controller called SliderController.php .

I wrote this code to make the user upload an image:

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

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);



    if(isset($_POST['Slider']))
    {
        $model->attributes=$_POST['Slider'];
        $model->image=CUploadedFile::getInstance($model, 'image');
        if($model->save()) {
            if(strlen($model->image)>0)
            {
                $model->image->saveAs(Yii::app()->basePath.'/../uploads/slider/'.$model->image);
            }


            $this->redirect(array('view','id'=>$model->id));
        }
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

and it works fine.

My question is: how can I get/return the filetype and the size in the view?

4

2 回答 2

1

CUploadedFile有你可以使用sizetype属性。由于信息在里面$model->image,你可以在视图中访问它

Size: <?= $model->image->size; ?>
Type: <?= $model->image->type; ?> 

请注意,上面的类型是由客户端报告的,因此对于错误配置的客户端(有意或无意),它将不准确。出于显示目的,这并不重要,但如果您将 MIME 类型存储在某处以供以后使用,您应该在服务器端使用CFileHelper::getMimeType.

于 2013-08-27T19:31:16.900 回答
0

您还可以使用 $model->image->getExtensionName() 获取扩展名。该方法返回文件扩展名。检查以下链接中的文档。

http://www.yiiframework.com/doc/api/1.1/CUploadedFile#getExtensionName-detail

于 2013-08-27T21:11:00.230 回答