0

I'm having trouble extracting file some files from zip using ZipArchive

Here is my code

$pp_saved_zip_archive = "$extract_path/archive.zip";
$zip = new ZipArchive;
$res = $zip->open($pp_saved_zip_archive, ZipArchive::CREATE);
$zip->extractTo($previews_path);
$zip->close();

I get two errors/warnings in this code

PHP Warning: ZipArchive::extractTo(): Invalid or uninitialized Zip object in ...
PHP Warning: ZipArchive::close(): Invalid or uninitialized Zip object in ...

If I check for $res variable it returns 19, which is weird, it should be either TRUE or FALSE

My paths are correct, so it's not a path problem. This is done behind WordPress, I've tried $_SERVER['DOCUMENT_ROOT'] and ABSPATH to no avail. I also tried with different zip files, still no luck.

ivan marchenko
  • 401
  • 1
  • 4
  • 15

1 Answers1

-2
$result = $zipArchive->open("The archive you want to open");
if ($result === TRUE) {
    $zipArchive ->extractTo("my_dir");
    $zipArchive ->close();
    // Do something else on success
} else {
    // Do something on error
}

Try this Source: https://stackoverflow.com/a/8889073/5717344

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 27 '22 at 08:20