How to extract zip file in laravel?

by elisha_langworth , in category: PHP Frameworks , 4 months ago

How to extract zip file in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 4 months ago

@elisha_langworth 

To extract a zip file in Laravel, you can use the built-in functions provided by PHP for handling zip files. Here's a simple example of how to extract a zip file in Laravel:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
use ZipArchive;

public function extractZipFile($zipFilePath, $extractPath)
{
    $zip = new ZipArchive;
    if ($zip->open($zipFilePath) === TRUE) {
        $zip->extractTo($extractPath);
        $zip->close();
        return "Zip file extracted successfully";
    } else {
        return "Failed to extract zip file";
    }
}


In this example, the extractZipFile function takes in two parameters - the path to the zip file and the path where you want to extract the contents of the zip file. It uses the ZipArchive class provided by PHP to open the zip file, extract the contents to the specified extract path, and then close the zip file.


You can call this function in your Laravel application to extract a zip file. Simply pass in the path to the zip file you want to extract and the path where you want to extract the contents.