>  기사  >  백엔드 개발  >  PHP下载文件代码【可自命名】

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

WBOY
WBOY원래의
2016-07-25 08:42:21927검색
  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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.