首頁 >後端開發 >php教程 >php實作檔下載!

php實作檔下載!

WBOY
WBOY原創
2016-07-25 08:41:57865瀏覽

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