Shafizadeh Shafizadeh - 28 days ago 12
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');
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?

Answer

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