开发者

save variable as txt but not in the server

how can i save开发者_StackOverflow社区 some variables in a txt file for the user? dont want to generate and store in my server, just want to generate and then the user save on his pc, nothing changes on the server

btw, is this operation heavy on resources?

thanks


You can do that like this:

header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename=downloadedfile.txt');
echo "Text stuff! Cool!";

If the user loads this page, a Save As-window comes up, asking where to save "downloadedfile.txt", which will have the content "Text stuff! Cool!"


You can hint to the browser that your response is a file which should be saved by using a Content-Disposition header:

header('Content-Disposition: attachment; filename="filename.txt"');
header('Content-Type: text/plain');
echo "Desired file contents";

Note that this does imply you are generating the file on the server, which your question suggests isn't what you want. You could build up the file contents in Javascript on the client side, then create a link using the data uri scheme to allow download. However, it's not support by all browsers, you can't force a "save as", and there are some size limits on what you can generate.


header('Content-Type: text/plain');
echo "Text stuff! Cool!";

(For some reason, SO stripped my opening php-tag.)


I am not sure I understand your question correctly, but if you want to store information on the client side and retrieve it again later when the user returns you could handle state using:

  • cookies
  • DOM storage (not as widely supported)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜