【问题标题】:How can I make a downloadable .xml file? [duplicate]如何制作可下载的 .xml 文件? [复制]
【发布时间】:2016-11-09 11:40:23
【问题描述】:

这是我的代码:

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 可以做到这一点吗?

【问题讨论】:

    标签: php xml


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-19
        • 1970-01-01
        • 2013-03-12
        • 1970-01-01
        • 2010-12-27
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多