Search code examples
phplaravelzip

How to delete laravel delete file after extract?


I am using Zipper to extract uploaded zip file and delete the file after extract. So I upload and extract like this:

$f = $request['file']->move(public_path($directory), $fullFileName);
\Zipper::make($f)->extractTo(public_path($directory) . $fileName);

and it works great. I've tried to delete the file using these ways.

1 - Storage::disk('products')->delete($fullFileName);
2 - File::delete(public_path($directory) . $fullFileName);
3 - $del = unlink(public_path($directory) . $fullFileName);

but in all actions get resource temporarily unavailable error. I found this error is because of the zipper (simple files and directories works).

so my question is, How can I delete upload zip file after extract, using a zipper?

Any idea would be great. thanks in advance.


Solution

  • You need to call $zipper->close(); after you extract it, so if you do something like this it should work:

    $zipper = new \Chumper\Zipper\Zipper;
    $zipper->make($f)->extractTo(public_path($directory) . $fileName);
    $zipper->close();
    unlink(public_path($directory) . $fullFileName);
    

    If you do not close the zipper it will not write the result to the disk and keep the original file locked. See the documentation.