download.init.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.2 国产PHP开发框架 扩展类库-下载类
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class downloadInit {
  12. private $allow = array(".jpg", ".txt", ".gif", ".png", ".rar"); //允许下载的文件类型
  13. /**
  14. * 文件下载
  15. * @param string $file_name 文件名
  16. * @param string $server_path 文件目录
  17. * @param string $mime_type 传输类型
  18. * @return
  19. */
  20. public function down($file_name, $server_path = './', $mime_type = 'application/octet-stream') {
  21. $full_file_name = $server_path . '/' . $file_name;
  22. $this->check_file_ext($file_name);
  23. $this->check_file_exists($full_file_name);
  24. header("Content-Type: {$mime_type}");
  25. $file_name = '"' . htmlspecialchars($file_name) . '"';
  26. $file_size = filesize($full_file_name);
  27. header("Content-Disposition: attachment; filename={$file_name}; charset=utf-8");
  28. header("Content-Length: {$file_size}");
  29. readfile($full_file_name);
  30. exit;
  31. }
  32. /**
  33. * 检测文件类型
  34. * @param string $file_name 文件名
  35. * @return
  36. */
  37. private function check_file_ext($file) {
  38. $file_ext = strtolower(substr($file, -4));
  39. if (!in_array($file_ext, $this->allow)) exit('this file is deny!');
  40. return true;
  41. }
  42. /**
  43. * 检测文件是否存在
  44. * @param string $full_file_name 带目录的文件名
  45. * @return
  46. */
  47. private function check_file_exists($full_file_name) {
  48. if (!file_exists($full_file_name)) exit('this file does not exit!');
  49. return true;
  50. }
  51. }