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

在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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化