E
E
Evgeny Yakushov2019-10-09 19:41:27
PHP
Evgeny Yakushov, 2019-10-09 19:41:27

How to download a dynamically generated file and make the location of this page?

I have a file dynamically created with some text, you need to download it, and then do header("Location").
I do not understand how to do this, because I send the browser a header:

header('Content-disposition: attachment; filename=test.txt');
header('Content-type: text/plain');
echo "Что-то пишем в файл";

header('Location: ' . $_SERVER["REQUEST_URI"]);

And of course it gives an error, because the text is already being output, and after that we still send headers
. You can somehow fix this or offer alternative options (I don’t really want to store the file on the server)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
PrAw, 2018-07-06
@SANYAAAASSSS

Your problem is that you give the UTF-8 encoding, and place the text in 1251 encoding .
Options:
1. kruslan 's answer - change the encoding to UTF-8 of the received text
2. header('Content-Type: text/html; charset= windows-1251')
somewhere at the very beginning of the code before any text is printed.

E
Evgeny Yakushov, 2019-10-10
@yevgenyyakushov

Grigory Vasilkov , in general, did it through js, but I don’t know how good it is, I just don’t see any more options, because unique values ​​​​are generated, and then go to hash, after which I do a js hook

<a download="password.txt" id="data" href="#" style="display: none;"></a>
<script>
  var type = "data:application/octet-stream;base64, ";
  var text = "Данные для авторизации \\r\\nЛогин: ' . $login . ' \\r\\nПароль: ' . $password . '";
  var base = btoa(unescape(encodeURIComponent(text)));
  var res = type + base;
  document.getElementById("data").href = res;
  document.getElementById("data").click();
  location.href=location.href;
</script>

G
Grigory Vasilkov, 2019-10-09
@gzhegow

unfortunately only javascript. you can't send a file and then redirect it. the return of the file is code 200 - everything is ok. and the redirect is 301
action logic code - try to do it like on megashare mine or some such well-known exchanger that gives the file in its own way, I suppose it somehow transfers it piece by piece to javascript via a websocket, probably, and then connects the pieces of the file already on the client and download instantly. and after that instant download - redirect person

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question