<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Quick Solution Providers &#187; linux</title>
	<atom:link href="http://www.quicksolutionproviders.com/tag/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.quicksolutionproviders.com</link>
	<description>Complete Solution for your problems under one roof</description>
	<lastBuildDate>Sun, 22 Aug 2010 08:23:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to unzip a zip file using php</title>
		<link>http://www.quicksolutionproviders.com/php/how-to-unzip-a-zip-file-using-php/</link>
		<comments>http://www.quicksolutionproviders.com/php/how-to-unzip-a-zip-file-using-php/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 12:38:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[chmod]]></category>
		<category><![CDATA[delete zip]]></category>
		<category><![CDATA[directory permission]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mkdir]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[unzip]]></category>
		<category><![CDATA[zip]]></category>
		<category><![CDATA[zip_open]]></category>

		<guid isPermaLink="false">http://www.quicksolutionproviders.com/?p=312</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Very exciteing and challenging work!.You are thinking that what is exciteing and challenging in this work but i am <strong>talking about how to unzip a zip file using php</strong>.<br />
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.</p>
<p>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<br />
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.<br />
<span id="more-312"></span><br />
&lt;?php<br />
function unzipnew($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true, $delzip=true)<br />
{<br />
if ($zip = zip_open($src_file))<br />
{<br />
if ($zip)<br />
{<br />
$splitter = ($create_zip_name_dir === true) ? &#8220;.&#8221; : &#8220;/&#8221;;<br />
if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)).&#8221;/&#8221;;<br />
// else $dest_dir = $desdir.&#8221;/&#8221;;</p>
<p>// Create the directories to the destination dir if they don&#8217;t already exist<br />
create_dirs($dest_dir);</p>
<p>// For every file in the zip-packet<br />
while ($zip_entry = zip_read($zip))<br />
{<br />
// Now we&#8217;re going to create the directories in the destination directories<br />
// If the file is not in the root dir<br />
$pos_last_slash = strrpos(zip_entry_name($zip_entry), &#8220;/&#8221;);<br />
if ($pos_last_slash !== false)<br />
{<br />
// Create the directory where the zip-entry should be saved (with a &#8220;/&#8221; at the end)<br />
create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));<br />
}<br />
// Open the entry<br />
if (zip_entry_open($zip,$zip_entry,&#8221;r&#8221;))<br />
{</p>
<p>// The name of the file to save on the disk<br />
$file_name = $dest_dir.zip_entry_name($zip_entry);<br />
// Check if the files should be overwritten or not<br />
if ($overwrite === true || $overwrite === false &amp;&amp; !is_file($file_name))<br />
{<br />
// Get the content of the zip entry<br />
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));</p>
<p>file_put_contents($file_name, $fstream );<br />
// Set the rights<br />
chmod($file_name, 0777);<br />
echo &#8220;save: &#8220;.$file_name.&#8221;&lt;br /&gt;&#8221;;<br />
}<br />
// Close the entry<br />
zip_entry_close($zip_entry);<br />
}<br />
}<br />
// Close the zip-file<br />
zip_close($zip);<br />
if($delzip) unlink($src_file);<br />
}<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
return true;<br />
}</p>
<p>/** * This function creates recursive directories if it doesn&#8217;t already exist<br />
* * @param String  The path that should be created *  * @return  void */<br />
function create_dirs($path)<br />
{<br />
if (!is_dir($path))<br />
{<br />
$directory_path = &#8220;&#8221;;<br />
$directories = explode(&#8221;/&#8221;,$path);<br />
array_pop($directories);</p>
<p>foreach($directories as $directory)<br />
{<br />
$directory_path .= $directory.&#8221;/&#8221;;<br />
if (!is_dir($directory_path))<br />
{<br />
mkdir($directory_path);<br />
@chmod($directory_path, 0777);<br />
}<br />
}<br />
}<br />
}</p>
<p>?&gt;</p>
<p>Lets talk one by one:<br />
The function &#8216;<strong>unzipnew</strong>&#8216; 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.<br />
<strong>syntex : unzipnew($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true,$delzip=true)</strong><br />
first argument is $src_file , this is the full path of zip file to which we have to unzip.<br />
second argument is $dest_dir and that should be false every time.<br />
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.<br />
forth argument is $overwrite means if there is already a folder with same name than we have to overwrite the or not.<br />
Fifth is $delzip measn after unzip we have to delete the zip file or not.If true then it will delete the zip.</p>
<p>Now second function is <strong>create_dirs($path)</strong>.This function has only one argument adn we are using this function in &#8216;unzipnew&#8217; function to create directory structure .You can use this function independently if you want.</p>
<p><strong>Now I show you how to use these functions below:</strong></p>
<p>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 &#8216;unzipnew&#8217; with all required parameters.</p>
<p><strong>&lt;?php<br />
include(&#8221;functions/zip_funct.php&#8221;);<br />
$dirLocal =&#8217;/home/work/anoop/test.zip&#8217;;<br />
unzipnew($dirLocal, false, true, true,true)<br />
?&gt;</strong></p>
<p>In the function &#8216;create_dirs&#8217; 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.</p>
<p><strong>Important note: we are using zip functions of php&#8217;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.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.quicksolutionproviders.com/php/how-to-unzip-a-zip-file-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
