Index.php 1.7 KB

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