php unzip to other dir

Posted on 2013-01-31
Last Modified: 2013-02-06
i been using the unzip code for about a year and it works great. But now i want to have the code the unzip the file to a dir i want and not in the dir the file is in.

copy('', '../Demo/');

$file = '../Demo/';

function unzip($file){

    if(!$zip) {return("Unable to proccess file '{$file}'");}


    while($zip_entry=zip_read($zip)) {

       if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file '{$zname}'";continue;}
       if(!is_dir($zdir)) mkdirr($zdir,0777);

       #print "{$zdir} | {$zname} \n";

       if(empty($zip_fs)) continue;





function mkdirr($pn,$mode=null) {

  if(is_dir($pn)||empty($pn)) return true;
  $pn=str_replace(array('/', ''),DIRECTORY_SEPARATOR,$pn);

  if(is_file($pn)) {trigger_error('mkdirr() File exists', E_USER_WARNING);return false;}

  if(mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} }
  return false;


Open in new window

Question by:armasmike
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38842928
see this

fopen and fwrite should come after using fopen of the required directory, in that way you can place the files in specified folder

Expert Comment

ID: 38843170
Have you tried zip_extractto( extract directory)
You can have a  look here
for details.
LVL 108

Expert Comment

by:Ray Paseur
ID: 38843584
This may be oversimplifying the question, but why not just move the ZIP archive to the destination directory location you want, then extract the files right there?

Author Comment

ID: 38844508

I was wanting to do just that. this is why i have

copy('', '../Demo/');

$file = '../Demo/';

Open in new window



Open in new window

at the bottom. I was thinking it would do just that unzip in the Demo dir. But it doesn't what am i missing.
LVL 108

Accepted Solution

Ray Paseur earned 500 total points
ID: 38844522
I don't know what you're missing and I don't really have anything other than suggestions (no time to test this process any time soon).  So here is my suggestion.  

1. Unzip the archive wherever it happens to be.  That part already works, so no magic is needed.

2. After Unzip, copy the files to the destination directory.

3. Unlink the files and the ZIP archive.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML code not showing slider block in Magento 2 29
Hacked File Timestamps 4 49
unset shopping cart session 15 29
PHP: Insert Data into MySQL 5 7
This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now