| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace app\index\controller;
- use app\api\model\Channel;
- use app\common\controller\Frontend;
- class Index extends Frontend
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- protected $layout = '';
- public function index()
- {
- echo phpinfo();
- // return $this->view->fetch();
- }
- /**
- * @param null $channel_key
- * @throws \think\Exception
- */
- public function download($channel_key = null)
- {
- $file_name = "__UNI__D48D872_0515172908.zip";//需要下载的文件
- $file_path = ROOT_PATH . DS . "public" . DS . "uploads" . DS . "download" . DS . $file_name; //文件目录
- $file_name = iconv("utf-8", "gb2312", "$file_name");
- $fp = fopen($file_path, "r+");//下载文件必须先要将文件打开,写入内存
- if (!file_exists($file_path)) {//判断文件是否存在
- echo "文件不存在";
- exit();
- }
- $file_size = filesize($file_path);//判断文件大小
- //返回的文件
- Header("Content-type: application/octet-stream");
- //按照字节格式返回
- Header("Accept-Ranges: bytes");
- //返回文件大小
- Header("Accept-Length: " . $file_size);
- //弹出客户端对话框,对应的文件名
- Header("Content-Disposition: attachment; filename=" . $file_name);
- //防止服务器瞬时压力增大,分段读取
- $buffer = 1024;
- while (!feof($fp)) {
- $file_data = fread($fp, $buffer);
- echo $file_data;
- }
- if (!is_null($channel_key)) {
- Channel::increase(null, $channel_key, "download_count");
- }
- //关闭文件
- fclose($fp);
- }
- }
|