I am trying to zip the files in a folder. Example: In a path localhost/magento/var/log, there are two files system.log, debug.log. Following is the code which I tried to zip the files. Am developing this in MAGENTO 2. Am getting an error when adding a file to zipping.

$filePath = $this->_dir->getPath('log');//[/rootpath/var/log]
$logFileConfigurations = scandir($filePath, 1);//[system.log, debug.log]
if (is_array($logFileConfigurations)) {
    foreach ($logFileConfigurations as $configuration) {
        if ($dir = opendir($filePath)) {
            $file = $configuration;
            try {
                $zip = new ZipArchive();
                $fileName = $configuration . date("d-m-YH:i:s") . '.zip';
                $fileName = str_replace(" ", "", $fileName);

                if ($zip->open($fileName, ZIPARCHIVE::CREATE) === false) {
                    die ("An error occurred creating your ZIP file.");

            } catch (\Exception $e) {
                echo $e->getMessage();



Thanks in advance.


1 Answer 1


You are closing your zip file inside your loop.

Move $zip->close(); out side foreach and check again.

I hope it will work.

  • The Issue was the special characters which I use from "date and time" when creating a file. I replaced all special characters like " -,: " with empty space using str_replace(). And it worked!!
    – Octopus
    May 18, 2018 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.