CodeIgniter 3 Zip Encoding Class: create and download archives

A guide to the Zip Encoding Class in CodeIgniter 3, with archive and download methods.

CodeIgniter 3 Zip Encoding Class: create and download archives

A guide to the Zip Encoding Class in CodeIgniter 3, with archive and download methods.

Version matters

This tutorial covers CodeIgniter 3. The Zip Encoding Class and $this->load->library syntax are not available as the same library in CodeIgniter 4. For a new project, check the current version and migration guide before reusing older code.

Load and add data

In a CodeIgniter 3 controller, call $this->load->library('zip') and then $this->zip->add_data('note.txt', 'Example text'). You can supply an array of archive paths mapped to file contents, or use read_file and read_dir for existing files. Never let untrusted input choose arbitrary server paths.

Save or download

archive('/safe/directory/example.zip') writes a file and returns a success indicator; check that the destination is writable and inaccessible if private. download('example.zip') sends the archive to the browser; do not emit other output first. get_zip() returns the archive bytes, while clear_data() resets cached entries before a new archive.

Security and references

Authorize the request, restrict readable files and archive names, and set limits on size and resources. Official CodeIgniter 3 guide: https://codeigniter.com/userguide3/libraries/zip.html ; migration guide: https://codeigniter.com/user_guide/installation/upgrade_4xx.html

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0