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

10
  • Check file permissions Commented Oct 25, 2022 at 10:37
  • Does this answer your question? PHP Warning ZipArchive::extractTo(): Permission denied Commented Oct 25, 2022 at 10:38
  • Does this answer your question? Permission Denied Error using Laravel & Docker Commented Oct 25, 2022 at 10:41
  • @Justinas the php version is updated 8.1.7 Commented Oct 25, 2022 at 10:47
  • 1
    Hm. Go in bash again, cd to folder and when issue ls -lhart *, please edit your question with the output. Commented Oct 25, 2022 at 12:11

1 Answer 1

2

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 them 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.