[英]Creating zip archive in php
I'm trying to creat zip archives with php using this class: Site 我正在尝试使用此类使用php创建zip归档文件: 网站
The first problem: 第一个问题:
in the array of $files_to_zip
. 在
$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. 创建ZIP时,它们会创建档案路径的每个文件夹。 Example: ZIP > folder "path" > folder "to" > folder "app" > archives.
示例:ZIP>文件夹“路径”>文件夹“至”>文件夹“ app”>存档。
I want just the archives in the ZIP. 我只想要ZIP中的档案。
I don't know where I change the code or what is wrong. 我不知道在哪里更改代码或出了什么问题。 Help pls?
帮助请?
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 . 对于第二个问题,可以使用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";
}
first problem solution 第一个问题解决方案
foreach($files_to_zip as &$value){
$value = 'path/to/app/'.$value
}
or use array_walk 或使用array_walk
second problem: 第二个问题:
try to create tmp folder, copy every file to it and zip em. 尝试创建tmp文件夹,将每个文件复制到其中并压缩zip。
This is simpliest solution. 这是最简单的解决方案。
Don't forget to remove tmp files. 不要忘记删除tmp文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.