如何制作可下载的.xml文件?


How can I make a downloadable .xml file?

下面是我的代码:

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

当我运行上面的代码时,它会在当前浏览器页面中打印一些东西。但是我想把它做成一个.xml文件,可以下载。PHP能做到吗?

使用Content-Disposition: attachment头,您可以强制浏览器下载您的'文件'。

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

试试这个

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