首页 >后端开发 >php教程 >php实现文件下载!

php实现文件下载!

WBOY
WBOY原创
2016-07-25 08:41:57874浏览

实现php文件安全下载!

  1. public function downloads($name){
  2. $name_tmp = explode("_",$name);
  3. $type = $name_tmp[0];
  4. $file_time = explode(".",$name_tmp[3]);
  5. $file_time = $file_time[0];
  6. $file_date = date("Y/md",$file_time);
  7. $file_dir = SITE_PATH."/data/uploads/$type/$file_date/";
  8. if (!file_exists($file_dir.$name)){
  9. header("Content-type: text/html; charset=utf-8");
  10. echo "File not found!";
  11. exit;
  12. } else {
  13. $file = fopen($file_dir.$name,"r");
  14. Header("Content-type: application/octet-stream");
  15. Header("Accept-Ranges: bytes");
  16. Header("Accept-Length: ".filesize($file_dir . $name));
  17. Header("Content-Disposition: attachment; filename=".$name);
  18. echo fread($file, filesize($file_dir.$name));
  19. fclose($file);
  20. }
  21. }
复制代码
php


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn