How to download zip file in Codeigniter?

by cali_green , in category: PHP Frameworks , 10 months ago

How to download zip file in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 5 months ago

@cali_green 

To download a zip file in CodeIgniter, you can use the built-in force_download function. This function will initiate a download prompt in the user's browser and allow the user to download the file. Here's an example of how to use it:

1
2
3
4
5
6
$this->load->helper('download');

$data = file_get_contents('path/to/your/zip/file.zip'); // Read the file's contents
$name = 'name_of_your_file.zip';

force_download($name, $data);


This code will read the contents of the zip file and then send it to the user's browser as a download. The user will be prompted to save the file with the specified name.


Alternatively, you can also use the readfile function to read the contents of the file and send it to the browser, like this:

1
2
3
4
5
6
7
8
9
$this->load->helper('file');

$file = 'path/to/your/zip/file.zip';

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));

readfile($file);


This code will send the contents of the zip file to the browser with the appropriate headers, causing the browser to download the file.