Solved

Create a zip without store the path

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Link SQL table to Webpage 9 39
php image upload 3 27
php variable basic question 12 29
File not deleting using unlink 6 14
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 …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

747 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

13 Experts available now in Live!

Get 1:1 Help Now