Home >Backend Development >PHP Tutorial >PHP implementation file forced download code

PHP implementation file forced download code

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:54:53948browse
  1. /**
  2. * php force download file
  3. * edit: bbs.it-home.org
  4. */
  5. $file_dir = "./";
  6. $name = "test.txt";
  7. $file = fopen($file_dir.$name," r");
  8. Header("Content-type: application/octet-stream");
  9. Header("Accept-Ranges: bytes");
  10. Header("Accept-Length: ".filesize($file_dir . $name) );
  11. Header("Content-Disposition: attachment; filename=".$name);
  12. echo fread($file, filesize($file_dir.$name));
  13. fclose($file);
  14. ?>
Copy the code

The code is very simple. It mainly focuses on learning the principles and implementation methods of forced downloading of files in PHP. You can improve on this basis.



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