0

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;
            chdir($filePath);
            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.");
                }

                $zip->addFile($file);
                $zip->close();
                unlink($file);
            } catch (\Exception $e) {
                echo $e->getMessage();
            }

        }

    }
}

Thanks in advance.

0
0

You are closing your zip file inside your loop.

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

I hope it will work.

1
  • 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 '18 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.