Because the current browser can recognize the file format in txt, if you just make a text link to the txt file, you may just open a new window to display the content of the txt file, click Download cannot be implemented. We have to do something else.
Of course, the solution to this problem can also be that you change your txt file to a file that the browser does not know, such as rar. If you click this file, the browser will have to download the file if you do not know it.
The following file sets the header to set the document format for click Download. You can click to download the file passed on the previous page.
<? Php
$ Filename = "/somepath/". $ _ GET [file]. ". txt"; // file name to be downloaded
Header ("Content-Type: application/force-download ");
Header ("Content-Disposition: attachment; filename =". basename ($ filename ));
Readfile ($ filename );
?>
First, set the Content-Type value to application/force-download and force download.
The second header function sets the file to be downloaded. Note that filename is a file name that does not contain a path, so use basename to filter out the path name. The value of this filename will be the file name in the pop-up dialog box after you click Download.
The last step is readfile, which outputs the file stream to the browser, so that the txt file can be downloaded. Other types are similar.