文件下载:Yii自带的文件下载功能

2018-05-03  本文已影响49人  lookphp
/* 
 * 下载 
 */  
public function actionDownload($id){  
    if ( isset($_GET['id']) ) {  
        $model = new Model();//你的model  
        $result = $model->find(array(  
            'select' => array('字段1', '字段2'),  
            'condition' => 'id=:id',//条件  
            'params'    => array(':id' => $id)  
            ));  
        if (!$result) {  
            throw new CHttpException(404, '文件不存在!');  
        }  
        else {  
            // 服务器端文件的路径   
            $fontArr  = explode('/', $result->url);  
            $fileName = end($fontArr); //得到文件名字  
            if (!empty($path) && file_exists($result->url)){   
                //发送两个参数一个是名称上面已经处理好,也可以改成你要的,后面是文件路径  
                yii::app ()->request->sendFile ($fileName,  file_get_contents ($result->url));  
            }   
        }  
    }  
}  

如果你不需要查数据库的话直接做参数传递就好了  
yii::app ()->request->sendFile (文件名,  file_get_contents (文件路径));  
上一篇下一篇

猜你喜欢

热点阅读