Home >Backend Development >PHP Tutorial >PHP file download!

PHP file download!

WBOY
WBOYOriginal
2016-07-25 08:41:57868browse

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn