How to unzip a zip file using php

Very exciteing and challenging work!.You are thinking that what is exciteing and challenging in this work but i am talking about how to unzip a zip file using php.
Some times programmer has the requirment to select a zip file and unzip all the files and folder at a particular location and then delete that zip file.

I faced this problem first time when i was making a file manager in php . That was not just a file manger but also a full featured ftp client.I tried to search on
google but not found anything .finally i got a small help from google and than i made a good function for you all to unzip a zip file.

<?php
function unzipnew($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true, $delzip=true)
{
if ($zip = zip_open($src_file))
{
if ($zip)
{
$splitter = ($create_zip_name_dir === true) ? “.” : “/”;
if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)).”/”;
// else $dest_dir = $desdir.”/”;

// Create the directories to the destination dir if they don’t already exist
create_dirs($dest_dir);

// For every file in the zip-packet
while ($zip_entry = zip_read($zip))
{
// Now we’re going to create the directories in the destination directories
// If the file is not in the root dir
$pos_last_slash = strrpos(zip_entry_name($zip_entry), “/”);
if ($pos_last_slash !== false)
{
// Create the directory where the zip-entry should be saved (with a “/” at the end)
create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}
// Open the entry
if (zip_entry_open($zip,$zip_entry,”r”))
{

// The name of the file to save on the disk
$file_name = $dest_dir.zip_entry_name($zip_entry);
// Check if the files should be overwritten or not
if ($overwrite === true || $overwrite === false && !is_file($file_name))
{
// Get the content of the zip entry
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

file_put_contents($file_name, $fstream );
// Set the rights
chmod($file_name, 0777);
echo “save: “.$file_name.”<br />”;
}
// Close the entry
zip_entry_close($zip_entry);
}
}
// Close the zip-file
zip_close($zip);
if($delzip) unlink($src_file);
}
}
else
{
return false;
}
return true;
}

/** * This function creates recursive directories if it doesn’t already exist
* * @param String  The path that should be created *  * @return  void */
function create_dirs($path)
{
if (!is_dir($path))
{
$directory_path = “”;
$directories = explode(”/”,$path);
array_pop($directories);

foreach($directories as $directory)
{
$directory_path .= $directory.”/”;
if (!is_dir($directory_path))
{
mkdir($directory_path);
@chmod($directory_path, 0777);
}
}
}
}

?>

Lets talk one by one:
The function ‘unzipnew‘ unzip a zip file with same name and if file or folder already exists with same name then you have option to overwrite or not overwrite that.
syntex : unzipnew($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true,$delzip=true)
first argument is $src_file , this is the full path of zip file to which we have to unzip.
second argument is $dest_dir and that should be false every time.
Third one is $create_zip_name_dir.$create_zip_name_dir=true means code will create a sub directory with the same name as main directory, means if u are unzipping abcd.zip then directory structure will be abcd/abcd/subfolders and files.$create_zip_name_dir=false means directory structure will be same as zip means abcd/subfolders and files.
forth argument is $overwrite means if there is already a folder with same name than we have to overwrite the or not.
Fifth is $delzip measn after unzip we have to delete the zip file or not.If true then it will delete the zip.

Now second function is create_dirs($path).This function has only one argument adn we are using this function in ‘unzipnew’ function to create directory structure .You can use this function independently if you want.

Now I show you how to use these functions below:

first put these 2 functions in a file and include that file in main file like below.Then put a full path of that zip file in a variable and then call the function ‘unzipnew’ with all required parameters.

<?php
include(”functions/zip_funct.php”);
$dirLocal =’/home/work/anoop/test.zip’;
unzipnew($dirLocal, false, true, true,true)
?>

In the function ‘create_dirs’ we are making directory structure and giving 0777 permission to each directory so we can write folders and files in those directories.But may be your server has no permission to edit the rules then you have to give the 0777 permission using ftp or cpanel.

Important note: we are using zip functions of php’s php_zip.dll. so php_zip.dll must be installed on that server. php_zip.dll is different for windows and Linux so download latest dll according to server.

Advertisements
By Rz Rasel Posted in Php

2 comments on “How to unzip a zip file using php

  1. An fascinating dialogue is value comment. I think that it’s best to write extra on this subject, it won’t be a taboo subject but usually persons are not sufficient to speak on such topics. To the next. Cheers

  2. Hey there. I’m sorry to bother you but I happened to run across your blog site and noticed you are using the exact same template as me. The only issue is on my site, I’m battling to get the theme looking like yours. Would you mind contacting me at: Minchow8029@gmail.com so I can get this figured out. By the way I have bookmarked your internet site: https://arzrasel.wordpress.com/2012/07/30/how-to-unzip-a-zip-file-using-php/ and will certainly be visiting often. With thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s