How to extract zip file in Codeigniter?

by cortez.connelly , in category: PHP Frameworks , 8 months ago

How to extract zip file in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 3 months ago

@cortez.connelly 

To extract a zip file in CodeIgniter, you can use the ZipArchive class. Here is an example of how to extract a zip file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$this->load->library('zip');

// Open the zip file
$zip = new ZipArchive;
if ($zip->open('path/to/file.zip') === TRUE) {
    // Extract the zip file
    $zip->extractTo('path/to/extract/to/');
    $zip->close();
    echo 'Zip file extracted';
} else {
    echo 'Error extracting zip file';
}


Make sure to load the zip library before using it by calling the $this->load->library('zip') function.


You can also use the unzip function provided by the zip library to extract a zip file:

1
2
3
4
$this->load->library('zip');

// Extract the zip file
$this->zip->unzip('path/to/file.zip', 'path/to/extract/to/');


This will extract the contents of the zip file to the specified destination directory.