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

时间:2022-11-12 19:19:03

This question already has an answer here:

这个问题已经有了答案:

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