tags:

views:

5583

answers:

3

I've created a PHP DOM xml piece and saved it to a string like this:

<?php
  // create a new XML document 
  $doc = new DomDocument('1.0');
  ...  
  ...
  ...
  $xmldata = $doc->saveXML();
?>

Now I can't use the headers to send a file download prompt and I can't write the file to the server, or rather I don't want the file laying around on it.

Something like a save this file link or a download prompt would be good. How do I do it?

+1  A: 

You could enable output_buffering in your php.ini, then you might have some options with sending headers.

http://us.php.net/manual/en/function.headers-sent.php

Dan
+3  A: 

I see from the comments that you're working from within a CMS framework and are unable to stop content from being output prior to where your code will be.

If the script in which you're working has already output content (beyond your control), then you can't do what you're trying to achieve in just one script.

Your script can either send headers saying "the following content is HTML" then output the HTML or send headers saying "the following content is XML, is an attachment and has a certain filename". You can't do both.

You can either output HTML containing a link to a separate script for downloading an XML file or you can issue a file download and output no HTML.

Therefore, you'll have to add a download link in the output of the CMS script you're modifying and then handle the download in a separate script.

I have made a working example that should help. The example includes a simple HTML document containing a download link, and a PHP script that then handles the download.

View the code below or take a look at the live example.

HTML (extraneous fluff removed, not necessarily valid)

<html>
<head>
<title>XML Download Example</title>
</head>

<body>

<a href="download.php">Download XML example</a>

</body>
</html>

PHP

<?php
// Populate XML document
    $doc = new DomDocument();
    // ... various modifications to the document are made

// Output headers
    header('Content-type: "text/xml"; charset="utf8"');
    header('Content-disposition: attachment; filename="example.xml"');

// Output content
    echo $doc->saveXML();
?>

If you are fully unable to handle the download via a second script (perhaps you can't get access to the relevant data), you'll have to re-think the problem.

Jon Cram
I think you hit the spot. I was basically missunderstanding the concept of headers i.e. when you set headers you set filetype which gives the browser some clue on what to do. I was hoping I could work from within the CMS with its functinos not having an external file, but that doesn't seem possible.
Reed Richards
A: 

The same thing worked for me. But i am not using XML DOM. i use SimpleXML to parse my xml.

$xml = new SimpleXMLElement("<root></root>");

I want to give my view a custom xml which will be generated depending upon the data they give through the form. On submiting the form they get the xml as download with thier own desired name.

header('Content-type: "text/xml"; charset="utf8"');

header('Content-disposition: attachment; filename="'.$_POST['filename'].'.xml"');

echo $xml->asXML();

Goysar