Force user to download .txt (php, file.txt)

I have a small script that reads data from a database, massages it and saves it as a .txt file. At the end, the user is redirected to this file. Now, how to achieve this, when the user is redirected to .txt, the file download window will appear?

    <?php
// Preden zacnemo, dobi novico iz baze!
$MOD_NEWS_SAVETXT_getnews = mysql_query("SELECT * FROM NEWS WHERE NEWSid = '{$_GET['id']}'") or die(mysql_error());
// Nardimo while in priredimo vsebino iz baze spremenljivkam!
while ($MOD_NEWS_SAVETXT_NEWSRESULT = mysql_fetch_array($MOD_NEWS_SAVETXT_getnews)) {
    $MOD_NEWS_SAVETXT_FILE_name = $MOD_NEWS_SAVETXT_NEWSRESULT['NEWStitle'] . ".txt";
    echo $MOD_NEWS_SAVETXT_FILE_name;
    $MOD_NEWS_SAVETXT_FILE_handle = fopen($MOD_NEWS_SAVETXT_FILE_name, 'w') or die("Ne morem brati/ustvariti datoteke!");
    fwrite($MOD_NEWS_SAVETXT_FILE_handle, $MOD_NEWS_SAVETXT_NEWSRESULT['NEWStext']);
    fclose($MOD_NEWS_SAVETXT_FILE_handle);
    header("Location: ./" . $MOD_NEWS_SAVETXT_FILE_name ."");
}
?>
+3
source share
2 answers

, HTTP-, . Content-Type .

http://en.wikipedia.org/wiki/List_of_HTTP_headers

, - , , mime. HTTP Content-Type mime-, .

PHP, .

.

header("Content-type: application/force-download"); 

header("Content-type: application/octet-stream");

HTTP- Content-Disposition Content-Transfer-Encoding.

MIME: http://www.faqs.org/rfcs/rfc1521.html

, .

, . http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

+5

Source: https://habr.com/ru/post/1714508/


All Articles