2017-02-09 119 views
0

以下代碼無法提示下載xml文件。我如何設置$數據之前的頭,因爲我得到了如下警告: 「警告:不能更改頭信息 - 頭已經發出」編寫一個xml文件並強制用php下載

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

你有空格在你的文件的名字嗎?試試你的封裝在文件名引號doulbe像這樣:

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

上php.net的例子:http://php.net/manual/en/function.readfile.php

+0

沒有路易斯似乎並沒有工作。感謝您及時的回覆!! – suna