Solved

Create a zip without store the path

Posted on 2011-09-12
2
651 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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 …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

810 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