Sphinx
Sphinx

Reputation: 109

Chrome opens file instead of downloading if size > n Kb

I have to make a php-generated file to be downloadable, not openable in browser. While it is small (1-2 kb) file saves normally in Downloads, but after some point, Chrome starts opening it instead of downloading.

Here is controller's code:

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);
    }
}

Upvotes: 0

Views: 155

Answers (2)

Sphinx
Sphinx

Reputation: 109

Correct answer using CodeIgniter's helper:

$txt = 'file content';
$this->load->helper('download');
force_download('File ' . date('Y-m-d H:i:s') . '.txt', $txt);

Upvotes: 1

Calle Bergström
Calle Bergström

Reputation: 488

Have you tried adding the force download content-type header?

It can be done like this:

$this->output->set_header("Content-Type: application/force-download");

OR

header("Content-Type: application/force-download");

Upvotes: 0

Related Questions