Solved

Create a zip without store the path

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Functions in phpMyAdmin 8 26
Doing something wrong with this PDO Select Statement... 3 19
Link failure 16 35
Ajax and PHP 9 30
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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

828 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