Welcome Guest, Not a member yet? Register   Sign In
ZipArchive: class not found

I can't use the ZipArchive class in CI 4.0.0-rc.3, I can't find out a solution...
Thanks in advance for your support!

[Image: Deepin-Capture-cran-zone-de-s-lection-20...161136.png]

There is no ZipArchive in CI4, nor in CI3.
You are trying to open \ZipArchive, but the error message shows it can't find app\Controllers\Back\ZipArchive, which doesn't feel right. Could this be a namespace problem?

I just want to use the PHP ZipArchive class (https://www.php.net/manual/en/class.ziparchive.php).
I got the same problem when removing the backslash : $zip = new ZipArchive();
I can't manage to use it with CI4, I got no problem with CI3.
Any idea?

The problem is that the second reference to the ZipArchive class doesn’t have a prefix slash, so PHP thinks it is relative to the current namespace.

(This post was last modified: 11-26-2019, 11:21 AM by Poetawd.)

CI4 is awesome because you can use almost any library with it !

For working with ZIP Files I recommend this one: https://github.com/Ne-Lexa/php-zip

It is AWESOME and has some great features !

It is very simple to make it work with CI4. Easier with composer !

Please tell me if you need help installing it.

Here is a example how to use the library:

PHP Code:
        $projectName 'Project';

$projectDirectory ROOTPATH '/../folder';

$zipComments 'Some Comments';

$zipFile = new \PhpZip\ZipFile();

        try {            

setPassword('12345'// set password for all entries
->outputAsAttachment('filename.zip'); // output to the browser without saving to a file

} catch (\PhpZip\Exception\ZipException $e) {
// handle exception

This code will ZIP a entire folder, including sub-directories, set comments for the ZIP file, set a password and output it to browser without saving the file in server !

Awesome right ?

CI4 using php namespace, may be this affect for call class ZipArchive from Controller. I have solved this problem by way:
- Create helper, ex: zip_helper (file store in folder App/Helpers/) and define function ex: zipFile($fileAddPath, $fileZipPath):

PHP Code:
function zipFile($filePath)
      $zip = new ZipArchive;
    if ($zip->open($fileZipPath, ZipArchive::CREATE || ZipArchive::OVERWRITE) === TRUE) {
      echo 'ok';
   } else {
    echo 'failed';

- Load helper in controller:

PHP Code:
// ...


(This post was last modified: 05-05-2022, 11:05 AM by dgvirtual.)

For me using

$zip = new ZipArchive;


$zip = new \ZipArchive;

in controller method did not work, untill I added

use ZipArchive;

at the top of the controller. Then it started to work Smile

Donatas G.

Theme © iAndrew 2016 - Forum software by © MyBB