Strange behavior when trying to extract zip file in docker container using laravel
P粉665679053
P粉665679053 2024-01-07 15:06:24
0
1
587

I'm trying to extract a zip archive in a docker container application running Laravel 9 on PHP 8.1.7 and I'm running into a strange error.

So if you try this code in your 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 the archive were extracted correctly, but this error was returned:

Error exception ZipArchive::extractTo(/var/www/html/public/my_folder/my_file.xml): Operation failed: Operation not allowed

If I run the same code in php artisantinker it works.

Does anyone have a solution to this problem?

Doesn't appear to be a permissions related issue, the folder is created with 777 permissions 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

P粉665679053
P粉665679053

reply all(1)
P粉668804228

I encountered the exact same problem. On my end, I encountered this problem because I was extracting files from the directory of my Windows installation.

I mean /var//html/public/my_folder is a symbolic link to /mnt/dev/my_folder from Windows (C: e.g. \dev \my_folder).

Since the file system is different from Linux and Windows, it seems that something specific in the ZipArchive class causes this error.

I fixed this by extracting the files in /tmp/my_folder and then moving 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.';
}

Hope this helps.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template