Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laravel_ninja's avatar

Upload a zip file, extract its contents, and then store the extracted files in a storage location.

I need assistance with uploading a zip file containing a folder with PDF files. My goal is to extract the zip file and save all its contents, including the PDF files, to a storage location. I have attempted solutions using ZipArchive and PclZip, but I haven't found a satisfactory resolution. I appreciate any help in resolving this issue. Thank you

0 likes
1 reply
laravel_ninja's avatar

Here is my code

public function uploadAndExtract(Request $request)
    {

        $zipFile = $request->file('zip_file');
        $folderName = 'salaries-' . Carbon::now()->format('F') . "-" . rand(1000, 9999);
        $zipFilePath = $zipFile->path();

        Storage::makeDirectory($folderName);

        $zip = new ZipArchive;
        if($zip->open($zipFilePath) === True) {
            dd('Finally True');
            $zip->extractTo(storage_path("app/$folderName"));
            $zip->close();
        }

        Storage::delete($zipFile->path());

        return to_route('dashboard')->with('message', 'File uploaded and extracted successfully');
    }

Please or to participate in this conversation.