Solved

Create a zip without store the path

Posted on 2011-09-12
2
638 Views
Last Modified: 2012-06-21
Hi Experts,

I have the following function to create a zip archive
 
/* creates a compressed zip file */
function create_zip($url_mos='',$files = array(),$destination = '',$overwrite = true) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {
    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
      if(file_exists($file)) {
        $valid_files[] = $file;
      }
    }
  }
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
      return false;
    }
    //add the files
    foreach($valid_files as $file) {
      $zip->addFile($file,$file);
    }
echo "<a href='http://domain.com/raster/tmp/$url_mos.zip'><img src='img/download.gif'></a>";
unlink('tmp/'.$mos.'.sat');
unlink('tmp/'.$mos.'.gif');
    //debug
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
    
    //close the zip -- done!
    $zip->close();
    
    //check to make sure the file exists
    return file_exists($destination);
  }
  else
  {
    return false;
  }
}

$files_to_zip = array(
  'tmp/'.$mos.'.sat',
  'tmp/'.$mos.'.gif'

);
//if true, good and delete .gif and .sat files; if false, zip creation failed 
$result = create_zip($mos, $files_to_zip,'tmp/'.$mos.'.zip');

Open in new window


This function works great but I don't want to store the paths in archive.

I mean:
I want to zip two files (.gif and .sat) inside /tmp folder but when I run the function, the function create the zip archive under /tmp with this structure:
file.zip
|_tmp
   |____file.gif
   |____file.sat

and I want just thi
file.zip
|_file.gif
|_file.sat

It is possible?

Best regards

André Bolinhas
0
Comment
Question by:abolinhas
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36526196
line 26, instead of:
$zip->addFile($file,$file);

try:
$zip->addFile($file, basename($file) );
0
 
LVL 9

Author Closing Comment

by:abolinhas
ID: 36526270
Quick and easy :)
worked flawlessly
Many thanks
0

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

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

18 Experts available now in Live!

Get 1:1 Help Now