Home  >  Article  >  Backend Development  >  PHP code to implement browser click download of TXT document

PHP code to implement browser click download of TXT document

WBOY
WBOYOriginal
2016-07-25 08:59:14909browse
  1. /**
  2. * txt document click to download
  3. * edit bbs.it-home.org
  4. */
  5. $filename = '/path/'.$_GET['file'].'.txt'; //File path
  6. header("Content -Type: application/force-download");
  7. header("Content-Disposition: attachment; filename=".basename($filename));
  8. readfile($filename);
  9. ?>
Copy code

Instructions: The first header function sets the value of Content-Type to application/force-download; The second header function sets the file to be downloaded. Note that filename here is a file name that does not include a path. The value of filename will be the file name in the dialog box that pops up after clicking download. If there is a path, the file name in the dialog box that pops up is unknown; Finally, the file stream is output to the browser through the readfile function, thus realizing the download of the txt file.



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