当前位置:Gxlcms > PHP教程 > php实现文件下载!

php实现文件下载!

时间:2021-07-01 10:21:17 帮助过:33人阅读

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

人气教程排行