-5

The below code creates a zip file in my own PHP directory, but how can I specify the output folder?

Also, when we extract the zip file, it contains the folder structure of files; how can I prevent this?

$zip=new ZipArchive;
$destination1="C:/testfolder/destination/*.*";
$zip->open('file.zip', ZipArchive::CREATE);
foreach (glob($destination1) as $filename)
{
    $zip->addFile($filename);
    echo 'ok';
}
Kingsters
  • 47
  • 1
  • 3
  • 8
  • 1
    and your question is? – Daniel A. White May 31 '13 at 12:07
  • zip file should be present in new directory. How to specify the new output directory for zip files??? – Kingsters May 31 '13 at 12:10
  • I got that too... if i extract the zip files it contains c folder then test folder then destination folderthen files... my expected output is zipfile should contain only files – Kingsters May 31 '13 at 12:21
  • Do you want to force users to expand the ZIP in a given directory in their computers? (as about the folder structure, just read the documentation for [ZipArchive::addFile()](http://php.net/manual/en/ziparchive.addfile.php)). – Álvaro González May 31 '13 at 12:22
  • Your second question (regarding removal of dirnames) might have been answered here: http://stackoverflow.com/questions/3993105/php-creating-zips-without-path-to-files-inside-the-zip – knb May 31 '13 at 12:28
  • In PHP, you want to visit [PHPNet](http://www.php.net/manual/) for all PHP documentation. You've lot of sample for using ZipArchive Class [here](http://www.php.net/manual/en/ziparchive.addfile.php). You want to read all user's comment. Globaly, I found all Answers with comments. – Doc Roms May 31 '13 at 12:23
  • Although you really are correct this isn't an answer, but a comment. – PeeHaa May 31 '13 at 12:35

1 Answers1

4
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //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);
        }
        //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;
    }
}

Sample Usage

$files_to_zip = array(
    'preload-images/1.jpg',
    'preload-images/2.jpg',
    'preload-images/5.jpg',
    'kwicks/ringo.gif',
    'rod.jpg',
    'reddit.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');

good luck

Leendert
  • 120
  • 8