download.init.php 2.1 KB

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