0

I have a PHP script that is producing an string that I want to download as a file by clicking a button.

I know how to save the string in a file on the web-server and point on it with a link, but that is not what I want to do.

I want by clicking a button to stream a file to the user that contains the string.

UPDATE: What I am looking for is something very close to this :

<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA">text file</a>

The Question that puts for me is how do I transform the $string into this base64 part and what is utf-16le ?? Can someone explain this a bit?

UPDATE actually some bad guys have felt into frustration that they are unable to answer and closed the topic. Without asking me. ome peple here take this a competition to gather points. But I went on and found the solution to my question.

You simple have to add download="file.txt"

<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA" download="file.txt">text file</a>

In this way you download a file. not a stream.

have fun.

  • 1
    Please add parts of your code you already tried, so we can have something to work with. – davejal Dec 16 '15 at 00:46
  • 1
    You need to save it on your server and then make a download link... – BananaBuisness Dec 16 '15 at 01:08
  • 1
    Please tell us how to stream the file, because it is too board. If you mean have the file data embedded, check this: http://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server – Sheepy Dec 16 '15 at 06:45
  • Great, the answer of Mathew Flaschen is what I am meaning, But I do not understand this part : base64,//5mAG8AbwAgAGIAYQByAAoA . I also tested it. but how do I make the string into the base64,//5mAG8AbwAgAGIAYQByAAoA ??? –  Dec 16 '15 at 19:34
  • 1
    http://stackoverflow.com/questions/600807/does-html-allow-for-embedded-encoding-of-images-by-stream – ivan_pozdeev Dec 16 '15 at 20:05
  • I found this : base64_encode in php, is this it? –  Dec 16 '15 at 20:10

2 Answers2

1

What you want to do is to save the file on your server and then just use html like this:

<a href="http://example.com/files/myfile.txt" target="_blank">Download</a>

Of course that the link will be changed dynamically by some js or jQuery (don't know how to do it? Go to https://stackoverflow.com/a/179717)

I'd send a respond from the php file that will contain the file name or path...

Also look at this answer

https://stackoverflow.com/a/2793756

Community
  • 1
  • 1
BananaBuisness
  • 339
  • 2
  • 18
0

This could be a solution:

<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="yourfilename.ini"');

//output the contents the file should contain. For example:
echo "a=1\nb=2\nc=3";
?>

Found it here

Community
  • 1
  • 1
FelixSFD
  • 6,052
  • 10
  • 43
  • 117