Home  >  Article  >  Backend Development  >  PHP download file code [can be named by yourself]

PHP download file code [can be named by yourself]

WBOY
WBOYOriginal
2016-07-25 08:42:21930browse
  1. Use the get method to pass in the parameters http://domain/download.php?file='savepath on server'&name='file name you want'
  2. //Pass in the file path and file name, used to read files and rename files respectively
  3. if( empty($_GET['file']) || empty($_GET['name'])){
  4. echo'<script> alert("Find File not found...!"); location.replace ("index.php") </script>'; exit();
  5. }
  6. //File path
  7. $file=$_GET['file '];
  8. //File name [i.e. the title of the file, not the name saved on the server]
  9. $name=$_GET['name'];
  10. //Get the file suffix
  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 "File does not exist! ";
  19. exit;
  20. }
  21. ?>
Copy code

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