这是第一个问题的解决方案
$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);
对于第二个问题,您可以使用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";
}
因此,总体而言,您的代码将如下所示:
<?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";
}