当我运行下面的函数时,它定位并读取文件,并在我的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