下面是我的代码:
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();