This question already has an answer here:
这个问题已经有了答案:
- PHP output to file for download without creating file on the server 2 answers
- PHP输出文件下载,无需在服务器2上创建文件
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?
当我运行上面的代码时,它会在当前浏览器页面中打印一些东西。但是我想制作一个。xml文件,可以下载。PHP能做到这一点吗?
2 个解决方案
#1
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();
#2
1
Try this
试试这个
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
echo $xml_contents;
exit();
#1
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();
#2
1
Try this
试试这个
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
echo $xml_contents;
exit();