现在,CSV文件将导出到报告文件夹中。相反,我希望浏览器询问将文件导出到哪里。这样我就可以将文件导出到客户端计算机的所需位置,并避免将那些文件存储在服务器(即,reports)文件夹中。
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$selProducts= $this->db->query("SELECT * FROM tablename");
$data= $this->dbutil->csv_from_result($selProducts);
$randomidgenerated = "reports/".date('Y-m-d').".csv";
if(file_exists(FCPATH.$randomidgenerated)){
$randomidgenerated = "reports/".date('Y-m-d')."_".random_string('alnum', 3).".csv";
}
if ( ! write_file($randomidgenerated, $data))
{
echo 'Unable to write the file';
}
$data = file_get_contents($randomidgenerated);
force_download($randomidgenerated, $data, TRUE);
从我所看到的,您正在创建要动态下载的文件的内容...现在要做的是强制浏览器将文件下载到客户端选择的部分,然后如果不这样做,则可能会删除临时文件。想要将其存储在服务器上...在创建文件或确定其存在之后添加下面的代码...之类的
$file = FCPATH.$randomidgenerated;
if(file_exists($file)){
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
那应该做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句