Solved

php unzip to other dir

Posted on 2013-01-31
5
329 Views
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('01-31-13_backup.zip', '../Demo/01-31-13_backup.zip');

$file = '../Demo/01-31-13_backup.zip';


function unzip($file){

    $zip=zip_open(realpath(".")."/".$file);
    if(!$zip) {return("Unable to proccess file '{$file}'");}

    $e='';

    while($zip_entry=zip_read($zip)) {
       $zdir=dirname(zip_entry_name($zip_entry));
       $zname=zip_entry_name($zip_entry);

       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";

       $zip_fs=zip_entry_filesize($zip_entry);
       if(empty($zip_fs)) continue;

       $zz=zip_entry_read($zip_entry,$zip_fs);

       $z=fopen($zname,"w");
       fwrite($z,$zz);
       fclose($z);
       zip_entry_close($zip_entry);

    }
    zip_close($zip);

    return($e);
}

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;}

  $next_pathname=substr($pn,0,strrpos($pn,DIRECTORY_SEPARATOR));
  if(mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} }
  return false;
}

unzip('../Demo/'.$file);

Open in new window

0
Comment
Question by:armasmike
5 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
Comment Utility
see this http://stackoverflow.com/questions/5256551/unzip-the-file-using-php-collapses-the-zip-file-into-one-folder

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

Expert Comment

by:rinfo
Comment Utility
Have you tried zip_extractto( extract directory)
You can have a  look here
http://php.net/manual/en/ziparchive.extractto.php
for details.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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?
0
 

Author Comment

by:armasmike
Comment Utility
Ray_Paseur

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

copy('01-31-13_backup.zip', '../Demo/01-31-13_backup.zip');

$file = '../Demo/01-31-13_backup.zip';

Open in new window


and

unzip('../Demo/'.$file);

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.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

11 Experts available now in Live!

Get 1:1 Help Now