简单的文件下载方法

$file_path = $this->request->get('url'); // baidu.com?url=filre/q12.jpg
            if(!$file_path)
                die('不存在的信息!');
            $base_path = $this->url->getBaseUri(); //框架更目录
            $info = explode(".",$file_path); //分割得到后缀名
            $suffix = $info[1];
            $file = @fopen($base_path.$file_path,"r");
            $filename = time();
            Header("Content-type: application/octet-stream"); 
            Header("Content-Disposition: attachment; filename={$filename}.{$suffix}"); 
            while (!feof ($file))  
                echo fread($file,50000); 
            fclose ($file); 
            die;

 

posted @ 2016-06-07 17:23  leisir  阅读(214)  评论(0编辑  收藏  举报