1

I'm trying to extract a zip archive in a docker container app running Laravel 9 on PHP 8.1.7, and i'm facing a weird error.

So if a try this code in a controller

    $zip = new ZipArchive();
    $result = $zip->open("/var/www/html/public/my_archive.zip");
    if ($result === TRUE) {
        $zip->extractTo("/var/www/html/public/my_folder");
    }
    $zip->close();

the files in archive are correctly extracted, but return this error:

ErrorException ZipArchive::extractTo(/var/www/html/public/my_folder/my_file.xml): Operation failed: Operation not permitted

If I run the same code in php artisan tinker it works.

Anyone have some idea to fix this problem?

It don't seem a permissions related problem, the folder is created with 777 permission and the files are copied correctly.

EDIT

  root@5899a5badc45:/var/www/html/public/my_folder# ls -lhart *
  -rwxrwxrwx 1 1000 1000 1.3K Oct 25 12:24 phpunit.xml

Thanks

cesare
  • 2,098
  • 19
  • 29
  • Check file permissions – Justinas Oct 25 '22 at 10:37
  • Does this answer your question? [PHP Warning ZipArchive::extractTo(): Permission denied](https://stackoverflow.com/questions/12446583/php-warning-ziparchiveextractto-permission-denied) – Justinas Oct 25 '22 at 10:38
  • Does this answer your question? [Permission Denied Error using Laravel & Docker](https://stackoverflow.com/questions/48619445/permission-denied-error-using-laravel-docker) – Plamen Nikolov Oct 25 '22 at 10:41
  • @Justinas the php version is updated 8.1.7 – cesare Oct 25 '22 at 10:47
  • `docker exec -ti your_image bash`, then copy zip file to folder `/var/www/html/public/my_folder` then `cd /var/www/html/public/my_folder/` and `unzip file.zip`. Does this work ? – Niloct Oct 25 '22 at 10:56
  • @Niloct Yes in command line works... – cesare Oct 25 '22 at 11:00
  • `/var/www/html/public/my_folder/my_file.xml`: seems like the parameter to `extractTo` you're calling is a `file` `my_file.xml` ?... Double-check your source code. The method call should be `$zip->extractTo("/var/www/html/public/my_folder");`, not `$zip->extractTo("/var/www/html/public/my_folder/my_file.xml");` – Niloct Oct 25 '22 at 11:16
  • the code: $zip->extractTo("/var/www/html/public/my_folder"); the error: ZipArchive::extractTo(/var/www/html/public/my_folder/phpunit.xml) – cesare Oct 25 '22 at 12:03
  • 1
    Hm. Go in `bash` again, `cd` to folder and when issue `ls -lhart *`, please edit your question with the output. – Niloct Oct 25 '22 at 12:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249039/discussion-between-cesare-and-niloct). – cesare Oct 25 '22 at 12:27

1 Answers1

0

I encoutered exactly the same problem. On my side I had this issue because I was extracting files in a directory that was mounted from Windows.

I mean /var//html/public/my_folder was a symlink of /mnt/dev/my_folder, that was coming from Windows (C:\dev\my_folder for example).

As filesystem are different from Linux and Windows, it seems like there might be something specific in the ZipArchive class that causes this error.

I fixed it by extracting files in /tmp/my_folder then move it to /var//html/public/my_folder.

$zip = new ZipArchive();
$res = $zip->open($filename);

if ($res === true) {
  $temp = '/tmp/my_folder';
  mkdir($temp, 0777, true);

  $zip->extractTo($temp);
  $zip->close();

  rename($temp, '/var/html/public/my_folder');
} else {
  echo 'Failed to open the zip file.';
}

I hope, this gonna be helpful.