PHP – ZIP-file creation results in ZIP-file zipping itself

one text

Solution:

AFTER "rotation" you need to call $file->page->files()

You need to manually "rotate" the zip archives, or just delete it (not recomended since it can result in data loss)

'file.create:after' => function ($file) {
      $page = $file->page();

      $parent_folder = $page->parent()->dirname(); // string
      $target_folder = $page->dirname(); // string

      $zip = new ZipArchive();

      $zip_file = "content/" . $parent_folder . "/" . $target_folder ."/somefile.zip";

      //delete old rotation if exists
      unlink($zip_file.'.backup');

      //rotate current zip file to '****.backup'
      rename($zip_file, $zip_file.'.backup');

      $files_to_zip = $file->page()->files(); // array

      $zip->open($zip_file, (ZipArchive::CREATE | ZipArchive::OVERWRITE));

      foreach ($files_to_zip as $file_to_zip) {
        $zip->addFile($file_to_zip->root(), basename($file_to_zip->filename()));
      }

      $zip->close();
}

Source