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.
function unzipnew($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true, $delzip=true)
if ($zip = zip_open($src_file))
$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
// 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
// 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
echo “save: “.$file_name.”<br />”;
// Close the entry
// Close the zip-file
/** * This function creates recursive directories if it doesn’t already exist
* * @param String The path that should be created * * @return void */
$directory_path = “”;
$directories = explode(”/”,$path);
foreach($directories as $directory)
$directory_path .= $directory.”/”;
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.
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.