Learn to Unzip a Zip file using PHP Script!

uniz-a-file-php

This post shows you how to Unzip a Zipped file using PHP. Its really simple using PHP ZIP extension.

Note this: This script works only If the ZIP extension installed in your PHP server.

PHP has built-in extensions for dealing with compressed files. Here is the script for you to play around:

<?php
//blog.theonlytutorials.com - unzip files using PHP
//author: agurchand

//create an object of ZipArchive Class
	$zip = new ZipArchive;
//open the file that you want to unzip. NOTE: give the correct path. In this example zip file is in the same folder	
	$zipped = $zip->open('phpzip.zip');

//give a folder name where the files has to be unzipped	
	$folder_name = 'unzip-folder';

//check if it is actually a Zip file	
	if ( $zipped ) {
//if yes then extract it to the said folder
	  $extract = $zip->extractTo($folder_name);

	  //if unzipped succesfully then show the success message
	  if($extract){
		echo "Your file extracted to $folder_name";
	  }
	  //close the zip
	  $zip->close();  
	}
?>

And also I have given a download for you to make it much easier 🙂

Download

 

Leave a Reply

Theme: Overlay by Kaira
Agurchand