欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php实现文件下载!

程序员文章站 2022-05-19 14:54:14
...

实现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