Search code examples
phpfilestream

How to make a download button that saves the content of a string?


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.


Solution

  • 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