1

I have created a script that outputs a string containing XML, Json or text. Now I want the visitor to be able to save it, but I don't want to save it on the server. It should only be saved by the visitor after the output content is generated.

Here is what I did:

if(isset($_POST['submit_down'])){
    $string = $_POST['result_text'];
    $type = $_POST['result_type'];
    if($type == "json") {
        header ("Content-Type: application/json");
        header ("Content-disposition: attachment; filename=names.json");
    } elseif($type == "xml") {
        header ("Content-Type: application/xml");
        header ("Content-disposition: attachment; filename=names.xml");
    } elseif($type == "text"){
        header ("Content-Type: text/plain");
        header ("Content-disposition: attachment; filename=names.txt");
    }
    echo $string;
    exit();
}

$string contains the content for the file.

The code I posted only displays the code in browser instead of saving it. What could be the problem?

Sergiu Neagu
  • 118
  • 1
  • 10
  • For such general questions, please just search, don't ask. In case you have a very special scenario not covered in existing Q&A material on site already, please give reference to the existing material and outline in concrete what didn't work for you and why. So it's clear how your question is different from existing material. Thanks & cheers. – hakre Apr 07 '15 at 22:26
  • This scenario is not covered in existing Q&A material. I only found how to output `.cfg` files, while I'm trying to export JSON, XML and text. – Sergiu Neagu Apr 08 '15 at 17:39
  • Right, regarding the *"What could be the problem?"* you might want to look for errors and warnings first: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) next up then is most likely this problem scenario [How to fix “Headers already sent” error in PHP](http://stackoverflow.com/q/8028957/367456) in any case and just for completeness, here is the [Reference - What does this error mean in PHP](http://stackoverflow.com/q/12769982/367456) - And: [Do I need Content-Type: application/octet-stream for file download?](http://stackoverflow.com/q/20508788/367456) – hakre Apr 08 '15 at 20:11

0 Answers0