Index.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace app\index\controller;
  3. use app\api\model\Channel;
  4. use app\common\controller\Frontend;
  5. class Index extends Frontend
  6. {
  7. protected $noNeedLogin = '*';
  8. protected $noNeedRight = '*';
  9. protected $layout = '';
  10. public function index()
  11. {
  12. return $this->view->fetch();
  13. }
  14. /**
  15. * @param null $channel_key
  16. * @throws \think\Exception
  17. */
  18. public function download($channel_key = null)
  19. {
  20. $file_name = "__UNI__D48D872_0515172908.zip";//需要下载的文件
  21. $file_path = ROOT_PATH . DS . "public" . DS . "uploads" . DS . "download" . DS . $file_name; //文件目录
  22. $file_name = iconv("utf-8", "gb2312", "$file_name");
  23. $fp = fopen($file_path, "r+");//下载文件必须先要将文件打开,写入内存
  24. if (!file_exists($file_path)) {//判断文件是否存在
  25. echo "文件不存在";
  26. exit();
  27. }
  28. $file_size = filesize($file_path);//判断文件大小
  29. //返回的文件
  30. Header("Content-type: application/octet-stream");
  31. //按照字节格式返回
  32. Header("Accept-Ranges: bytes");
  33. //返回文件大小
  34. Header("Accept-Length: " . $file_size);
  35. //弹出客户端对话框,对应的文件名
  36. Header("Content-Disposition: attachment; filename=" . $file_name);
  37. //防止服务器瞬时压力增大,分段读取
  38. $buffer = 1024;
  39. while (!feof($fp)) {
  40. $file_data = fread($fp, $buffer);
  41. echo $file_data;
  42. }
  43. if (!is_null($channel_key)) {
  44. Channel::increase(null, $channel_key, "download_count");
  45. }
  46. //关闭文件
  47. fclose($fp);
  48. }
  49. }