dstobkpm908182 2018-10-01 05:32
浏览 53
已采纳

在Codeigniter中提供文件的下载路径

So The thing in a table there are some checkboxes so based on the checkboxes user have checked it will downnload a particular coloumn of the table in the .php format. So I have tried the following code:-

public function export_php() {
            $export = $this->session->userdata('export_id');
            $exports = $this->query_revision_model->export($export);
            $data = "";
            foreach ($exports as $export) {
                $data .= $export->sql_changes . "
";
            }
            $filename = "export.php";
            $handle = fopen($filename, w);
            fwrite($handle, $data);
            fclose($handle);
    }

So,Everything is working fine but I'm unable to provide the path for the download files,So it's saving on the application folder which I don't want. I tried force_download($filename,$data) did n't work because I am using ajax.

  • 写回答

1条回答 默认 最新

  • duanchi1230 2018-10-01 05:54
    关注

    You need to force the browser to download file except display.

    public function export_php() {
            $export = $this->session->userdata('export_id');
            $exports = $this->query_revision_model->export($export);
            $data = "";
            foreach ($exports as $export) {
                $data .= $export->sql_changes . "
    ";
            }
            $filename = "export.php";
            $handle = fopen($filename, w);
            fwrite($handle, $data);
            fclose($handle);
            $filePath = APPPATH.$fileName;
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header("Content-Disposition: attachment; filename=$fileName");
            header("Content-Type: application/php");
    
    // Read the file
    readfile($filePath);
    exit;
    }
    

    and in your ajax code try this instead of ajax

    window.location.replace('Your URL Here')
    

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题