Search code examples
phplaraveldockerphp-ziparchive

Strange behaviour when try to extract a zip file in docker container with laravel


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


Solution

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