美文网首页
文件下载:Yii自带的文件下载功能

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

作者: lookphp | 来源:发表于2018-05-03 10:22 被阅读49次
    /* 
     * 下载 
     */  
    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 (文件路径));  

    相关文章

      网友评论

          本文标题:文件下载:Yii自带的文件下载功能

          本文链接:https://www.haomeiwen.com/subject/sxkzhftx.html