Unzip file on upload

Hello,

I’m trying to unzip an archive on upload.

  'hooks' => [
    'file.create:after' => function ($file) {
      if ($file->is('zip')) {
        f::unzip($file, $file->dir());
      }
    }
  ]

The panel throws an error :

Yet, dumping the $file->type() gives archive, which seems a valid file type.

Any idea ?

$file->is($file2) is for comparing one file $file to $file2 and expects a file object, not a string. Based on what you write about the dump’s result, if($file->type() == 'archive') should to the trick? Alternatively: if($file->extension() == 'zip').

1 Like

Thx @sebastiangreger !

No more error but the file isn’t unzipped with this code :

'file.create:after' => function ($file) {      
      if ($file->type() == 'archive') {        
        f::unzip($file, $file->contentFileDirectory() . '/test');
      }
    }

Try:

F::unzip($file->root(), $file->contentFileDirectory() . '/test');

unzip() expects the absolute path to the ZIP file, not the file object. The class name is F with a capital letter. I don’t have a way to test this right now, so this may or may not be working as such, but these are the two apparent issues here :slight_smile:

1 Like

It works ! Once again thank you for your help :slight_smile:

1 Like