写一个xml文件并强制下载php

时间:2017-02-09 06:14:43

标签: php xml

以下代码无法提示下载xml文件。如何在$ data之前设置标题,因为我收到以下警告: “警告:无法修改标题信息 - 标题已由”

发送
private function display() {
$data ="some string";
$my_file = "sample.xml"; // name of file to be downloaded
$handle = fopen($my_file, "w");
fwrite($handle, $data);
fclose($handle);
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename='.basename($my_file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($my_file));
readfile($my_file);
exit;
}
Here is my ajax function
$.ajax({
url: "webservices/rest_api/display",
        type: "POST",
        success: function(result) {
            console.log(result);
        }
    });

1 个答案:

答案 0 :(得分:1)

你的文件名中有空格吗?尝试将文件名封装在doulbe引号中,如下所示:

header('Content-Disposition: attachment; filename="'.basename($my_file).'"');

作为php.net上的示例:http://php.net/manual/en/function.readfile.php