當我運行下面的函數時,它定位並讀取文件,並在我的Chrome開發工具預覽選項卡中以csv格式正確顯示結果。但它不會下載它。如果我直接鏈接到我的瀏覽器中的文件,它會下載它,所以它似乎不是一個.htaccess問題。我使用了documentation中的示例以及它在Stack Overflow中找到的許多變體,但結果相同:該文件顯示在開發工具的預覽選項卡中(並且Firefox也是如此),但沒有下載。我的代碼:PHP讀取文件()不強制下載
public function download()
{
$file = $this->dir;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='. $file);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
我正在用本地最新的Wamp服務器進行開發。當我推/拉到我的遙控器時,結果是一樣的。
不是100%確定,但我認爲在調用ob_clean時,您的頭文件將被刪除。我會嘗試把ob_clean和flush在標題之前。 – James
嘗試使用來自php.net的示例代碼,它的工作原理。好像你遇到了AJAX問題,看到@ MajorCaiger的答案,就像擊敗了我。 – Lasse