亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

YII框架中的文件上傳功能怎么實現?

YII框架中的文件上傳功能怎么實現?

Yii
四季花海 2018-11-25 04:00:59
YII框架中的文件上傳功能怎么實現?
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊


文件上傳思路都是一樣的 都是獲取上傳文件,判斷類型,大小等等,一系列檢查通過后就可以上傳啦,php也有相應的上傳函數,yii則更加模塊化啦!具體操作下面我提供一個上傳圖片的列子

Model

1

2

3

4

5

6

7

8

9

10

11

12

class Xxxx extends CActiveRecord

{

    public $image;

    // ... other attributes

 

    public function rules()

    {

        return array(

            array('image', 'file', 'types'=>'jpg, gif, png'),//驗證上傳文件類型

        );

    }

}

View

1

2

3

4

5

<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

...

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

...

<?php echo CHtml::endForm(); ?>

Controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

class XxxxController extends CController

{

    public function actionCreate()

    {

        $model=new Item;

        if(isset($_POST['Item']))

        {

            $model->attributes=$_POST['Item'];

            //這里保存圖片名稱到數據庫

            $model->image=CUploadedFile::getInstance($model,'image');

            if($model->save())

            {

                $model->image->saveAs('path/to/localFile');//設置上傳路徑

                // redirect to success page

            }

        }

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

    }

}


 


查看完整回答
反對 回復 2018-12-08
  • 1 回答
  • 0 關注
  • 1008 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號