1

Here is my code:

public function xmlExport(){

    $xml = new \SimpleXMLElement('<xml/>');

    for ($i = 1; $i <= 8; ++$i) {
        $track = $xml->addChild('track');
        $track->addChild('path', "تست ");
        $track->addChild('title', "Track $i - Track Title");
    }

    Header('Content-type: text/xml');
    print($xml->asXML());
}

When I run code above, it prints something in the current browser page. But I want to make a .xml file of that which is downloadable .. Is doing that possible by PHP?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89

2 Answers2

4

With the Content-Disposition: attachment header, you can force the browser to download your 'file'.

<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
xmlExport();
take
  • 2,202
  • 2
  • 19
  • 36
1

Try this

header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');

echo $xml_contents;
exit();
Rafał Cz.
  • 735
  • 9
  • 19