Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create a zip without store the path

Posted on 2011-09-12
2
Medium Priority
?
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

704 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