Since current browsers can already recognize the txt document format, if you only make a text link to the txt document, it will only open a new window to display the content of the txt file after clicking, and the purpose of clicking to download cannot be achieved. Of course, the solution to this problem can also be to rename the txt file to a file that the browser does not recognize (such as rar). In this case, because the browser cannot recognize the rar type file, the user can only download it. Another way is to use code to set the format of the document through the header to achieve the purpose of click download.
The PHP code is as follows:
============================ ===============================
$filename = '/path/'.$_GET['file'] .'.txt'; //File path
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($filename)) ;
readfile($filename);
======================================== ======================
Brief description:
The first header function sets Content The value of -Type is 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 readfile function is used , output the file stream to the browser, thus realizing the download of txt file.