Igor Martins
Igor Martins

Reputation: 2047

Creating zip archive in php

I'm trying to creat zip archives with php using this class: Site

The first problem:

in the array of $files_to_zip. I need to add the path of the app in all of the dates, how i do this?

Convert this array:

$files_to_zip = array(
'preload-images/1.jpg',
'preload-images/2.jpg',
'preload-images/5.jpg',
'kwicks/ringo.gif',
'rod.jpg',
'reddit.gif'
  );

To this:

$files_to_zip = array(
'path/to/app/preload-images/1.jpg',
'path/to/app/preload-images/2.jpg',
'path/to/app/preload-images/5.jpg',
'path/to/app/kwicks/ringo.gif',
'path/to/app/rod.jpg',
'path/to/app/reddit.gif'
  );

The second problem:

When the ZIP was created, they create every folder of the path of the archive. Example: ZIP > folder "path" > folder "to" > folder "app" > archives.

I want just the archives in the ZIP.

I don't know where I change the code or what is wrong. Help pls?

Upvotes: 0

Views: 4788

Answers (2)

Alex Polly
Alex Polly

Reputation: 76

Here is the first problem solution

$files_to_zip = array(
'preload-images/1.jpg',
'preload-images/2.jpg',
'preload-images/5.jpg',
'kwicks/ringo.gif',
'rod.jpg',
'reddit.gif'
 );

$dir = 'c:/xampp/htdocs/test/'; 
$new_files = array();
  foreach($files_to_zip as $value){
   $new_files[] = $dir.$value;
}
print_r($new_files);

For second problem, you can use PCLZIP .

require_once('pclzip.lib.php');
// Create Object
$archive = new PclZip("compressed.zip");
/*
$compress = $archive->add($p_filelist, $p_option, $p_option_value, ...);

$files_archive = $archive->add($new_files, PCLZIP_OPT_REMOVE_PATH, $dir, PCLZIP_OPT_ADD_PATH, 'myFiles');
Here, 
$new_files = Array of files
PCLZIP_OPT_REMOVE_PATH = http://www.phpconcept.net/pclzip/user-guide/29
PCLZIP_OPT_ADD_PATH = http://www.phpconcept.net/pclzip/user-guide/28
*/
$files_archive = $archive->add($new_files, PCLZIP_OPT_REMOVE_PATH, $dir, PCLZIP_OPT_ADD_PATH, 'myFiles');
if ($files_archive == 0) {
    die("Error : ".$archive->errorInfo(true));
}   
else{
    echo "Archive Created";
}

So, overall you code will look like this:

<?php
require_once('pclzip.lib.php');
$files_to_zip = array(
'preload-images/1.jpg',
'preload-images/2.jpg',
'preload-images/5.jpg',
'kwicks/ringo.gif',
'rod.jpg',
'reddit.gif'
 );

$dir = 'c:/xampp/htdocs/test/'; 
$new_files = array();
  foreach($files_to_zip as $value){
   $new_files[] = $dir.$value;
}
print_r($new_files);
// Create Object
$archive = new PclZip("compressed.zip");
/*
$compress = $archive->add($p_filelist, $p_option, $p_option_value, ...);

$files_archive = $archive->add($new_files, PCLZIP_OPT_REMOVE_PATH, $dir, PCLZIP_OPT_ADD_PATH, 'myFiles');
Here, 
$new_files = Array of files
PCLZIP_OPT_REMOVE_PATH = http://www.phpconcept.net/pclzip/user-guide/29
PCLZIP_OPT_ADD_PATH = http://www.phpconcept.net/pclzip/user-guide/28
*/
$files_archive = $archive->add($new_files, PCLZIP_OPT_REMOVE_PATH, $dir, PCLZIP_OPT_ADD_PATH, 'myFiles');
if ($files_archive == 0) {
    die("Error : ".$archive->errorInfo(true));
}   
else{
    echo "Archive Created";
}

Upvotes: 2

knes
knes

Reputation: 883

first problem solution

foreach($files_to_zip as &$value){
   $value = 'path/to/app/'.$value
}

or use array_walk

second problem:

try to create tmp folder, copy every file to it and zip em.
This is simpliest solution.
Don't forget to remove tmp files.

Upvotes: 1

Related Questions