Shafizadeh Shafizadeh - 1 year ago 57
PHP Question

How can I make a downloadable .xml file?

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');

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

Answer Source

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

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