kkh
kkh

Reputation: 4869

Download file php

I have a Codeigniter function which puts some data in a file to be downloaded.

$data= "test";

header('Content-Disposition: attachment; filename=test.file');

echo $data;

However, the content includes the HTML for that view in which I click on to activate the function as well. How do I remove the content for the HTML inside the file?

Upvotes: 2

Views: 206

Answers (2)

MAK
MAK

Reputation: 775

Create a method in a controller

sample code:

  public function download_file($name)
    {
        $data = file_get_contents(FCPATH . 'storage' . DIRECTORY_SEPARATOR. $name); // Read the file's contents

        force_download($name, $data);
        redirect('index/index');
    }

Call the above method from view page

Upvotes: 0

Marcos
Marcos

Reputation: 1370

Create a separate controller/view just for the file.

Upvotes: 1

Related Questions