Through PHP browser download TXT document (turn)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Because now the browser can identify the txt document format, if only for the txt document as a text link words, after clicking just to open a new window displays the contents of the txt file, and click on the download of the objective can not be realized. Of course, the solution to this problem can also be txt files will be renamed the browser does not recognize the file (such as RAR), in this way, because the browser does not recognize the files of type rar, can let users download. Another approach is to use the code, set the document by the header format to achieve the purpose of click download.

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 the Content-Type value for the application/force-download;
second header function sets the file to download. Note that the filename does not include the path of the file name, the value of filename is to click the pop-up dialog box will download the file name inside, if with the path, the pop-up dialog file name is unknown;
the readfile function, file stream output to the browser, so as to realize the txt file download.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Armand at October 28, 2013 - 1:31 AM