我必须制作一个php生成的文件以便下载,而不能在浏览器中打开。虽然它很小(1-2kb),但文件在下载中正常保存,但过了一段时间,Chrome开始打开它而不是下载。
这是控制器的代码:
public function download($format, $str)
{
$get_string = rawurldecode($str);
parse_str($get_string, $get_array);
$webinar_id = $get_array['webinar_id'];
unset($get_array['webinar_id']);
$date = new DateTime();
$this->output->set_header('Content-type: text/plain');
$this->output->set_header('Content-Disposition: attachment; filename=Segmentation ' . $date->format('Y-m-d H:i:s') . '.' . $format);
if (! empty($get_array))
{
echo $this->segmentation_model->do_query($webinar_id, $get_array, $format);
}
}
使用CodeIgniter的助手进行正确回答:
$txt = 'file content';
$this->load->helper('download');
force_download('File ' . date('Y-m-d H:i:s') . '.txt', $txt);
您是否尝试添加强制下载内容类型标头?
可以这样做:
$this->output->set_header("Content-Type: application/force-download");
或
header("Content-Type: application/force-download");