Heim  >  Artikel  >  Backend-Entwicklung  >  PHP下载文件代码【可自命名】

PHP下载文件代码【可自命名】

WBOY
WBOYOriginal
2016-07-25 08:42:21930Durchsuche
  1. 采用get方式传入参数http://domain/download.php?file='savepath on server'&name='file name you want'
  2. //传入文件路径和文件名,分别用于读取文件和重命名文件
  3. if( empty($_GET['file']) || empty($_GET['name'])){
  4. echo'<script> alert("找不到文件了..... !"); location.replace ("index.php") </script>'; exit();
  5. }
  6. //文件路径
  7. $file=$_GET['file'];
  8. //文件名【即文件的标题,而非服务器上保存的名字】
  9. $name=$_GET['name'];
  10. //获取文件后缀
  11. $suffix = substr(strrchr($file, '.'), 1);
  12. if(is_file('./'.$file)) {
  13. header("Content-Type: application/force-download");
  14. header("Content-Disposition: attachment; filename=".$name.'.'.$suffix);
  15. readfile('./'.$file);
  16. exit;
  17. }else{
  18. echo "文件不存在!";
  19. exit;
  20. }
  21. ?>
复制代码

PHP


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn