在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.

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!