[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Function to unzip a zip file to a sub-directory

Posted on 2011-02-21
9
Medium Priority
?
192 Views
Last Modified: 2013-11-30
I'm trying to extract the contents of a zip package to a specific folder and keep getting an error.  I know the .zip package is good as I can open in from other programs.  This is php 5.2 so I know ZipArchive class is bundled.  Help???
Private $extractDir = './IMG/';

Private function download_pics ($dl_link){
	$zip = new ZipArchive;
	echo ('<br/>Start d/l Function');
	if  ($zip->open('./Test.zip') === true){
	  echo ('<br/>Start 1st Zip');
	  $zip->extractTo($extractDir);
	  $zip->close();
	  } ELSE {
  	  die("<br/>There was a problem unpacking the website .ZIP archive.");
	  }
  echo ('<br/>End Zip Functions');
  }

Open in new window

0
Comment
Question by:Dean_Kletter
8 Comments
 
LVL 8

Expert Comment

by:ragnarok89
ID: 34945518
is the php_zip.dll extension uncommented in your php.ini?
0
 
LVL 5

Expert Comment

by:wmadrid1
ID: 34945619
Can you please post te PHP Error
0
 

Author Comment

by:Dean_Kletter
ID: 34946455
I searched my php.ini and could not find a reference to php_zip.dll,  there was a mention of zlib but that appeared to be for uploading files.

I added:

extension=php_zip.dll

Still getting error, do I need to reboot?  Let IIS sit for 30 minute but did not restart services.

I have Error logging set to on and production level put nothing appeared but the error built into the code.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:wmadrid1
ID: 34946571
You need to restart the IIS,

You can verify the ZI functions are loaded, creating a file with
<?
echo phpinfo();
?>

And run it from internet explorer,
This show a list, with the status of PHP
and all the loaded plugins, here must be the ZIP libraries showed
0
 

Author Comment

by:Dean_Kletter
ID: 34946971
It says it is enabled

 screen shot of phpinfo
0
 
LVL 5

Accepted Solution

by:
wmadrid1 earned 2000 total points
ID: 34947040
I worked with this function to extract without problems:

function extractZip( $zipFile = '', $dirFromZip = '' )
{
	define(DIRECTORY_SEPARATOR, '/');    
	$zipDir = getcwd().DIRECTORY_SEPARATOR;
	$zip = zip_open($zipDir.$zipFile);    
	$zipDir = getcwd().DIRECTORY_SEPARATOR;
	if ($zip)    
	{        
		while ($zip_entry = zip_read($zip))        
		{            
			$completePath = $zipDir . dirname(zip_entry_name($zip_entry));            
			$completeName = $zipDir . zip_entry_name($zip_entry);            
			// Walk through path to create non existing directories            
			// This won't apply to empty directories ! They are created further below            
			if(!file_exists($completePath) && preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )            
			{                
				$tmp = '';                
				foreach(explode('/',$completePath) AS $k)                
				{                    
					$tmp .= $k.'/';                    
					if(!file_exists($tmp) )                    
					{                        
						mkdir($tmp, 0777);                    
					}                
				}            
			}            
			if (zip_entry_open($zip, $zip_entry, "r"))            
			{
				if( preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )                
				{                    
					if ($fd = fopen($completeName, 'w+'))                    
					{                        
						fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));                        
						fclose($fd);                    
					}                    
					else                    
					{                        
						// We think this was an empty directory                        
						mkdir($completeName, 0777);                    
					}                    
					zip_entry_close($zip_entry);                
				}            
			}        
		}        
		zip_close($zip);    
	}    
	return true;
}

Open in new window

0
 

Author Comment

by:Dean_Kletter
ID: 34952891
That is nice.  I don't need to enumerate through the ZIP file, just expand the contents to a folder.  The code below is directly off the PHP Website but still the ZIP file fails verification somehow.  I know the zip is valid, as I can open in numerous archiving application but it still fails the boolean test.
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo('/my/destination/dir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

Open in new window

0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 39686708
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question