PCLZIP is a great library, but unfortunately it is poorly documented. I am using it in order to support also servers where ZipArchive is disabled (or php version not supported)
I have a function to add uploaded files (one by one) to a ZIP archive. If the archive is not existing, it creates one, if the archive exists, it just adds the new files.
The problem I have is with a function adding a TXT file, that is based on the Comments from the archive. (the function reads the comments that were previously prepared, and should create a TXT file from string and insert into the archive.)
I can not seem to find a function to OVERWRITE a file from string (Or I do not know how to use it ).
I can create it with PCLZIP_ATT_FILE_NAME
,but somehow, when I run the function, it creates a new .txt
file (with the same filename!) every time it adds a file to the archive (as opposed to OVERWRITE the existing one)
I tried to use PCLZIP_ATT_FILE_NEW_FULL_NAME
- but I can not find where to give it the parameters to WHICH file it needs to overwrite ..
The function is here : (sorry if it is long..)
$archive = new PclZip($zipname);
if (!file_exists($zipname)){ //The Archive already exists - let´s just ADD new files.
$comment = $comment_head . $comment_add ;
$string_content = $comment;
$v_list = $archive->create($file,
PCLZIP_OPT_ADD_PATH, $sitename,
PCLZIP_OPT_COMMENT, $comment,
PCLZIP_OPT_REMOVE_ALL_PATH);
$prop = $archive->properties();
$prop = $prop['comment'];
if (!$prop) {$prop = $comment;}
$list = $archive->add(array(
array(
PCLZIP_ATT_FILE_NAME => $string_file,
PCLZIP_ATT_FILE_CONTENT => $prop,
PCLZIP_ATT_FILE_NEW_FULL_NAME => $string_file
)
)
);
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
} else {
// No Archive already exists - Create with new file .
$comment_add = $meta['file'] . PHP_EOL . PHP_EOL ;/*.$comment_foot*/ ;
$b_list = $archive->add($file,
PCLZIP_OPT_ADD_PATH, $sitename,
PCLZIP_OPT_ADD_COMMENT, $comment_add,
PCLZIP_OPT_REMOVE_ALL_PATH);
$prop = $archive->properties();
$prop = $prop['comment'];
if (!$prop) {$prop = $comment;}
$list_6 = $archive->add(array(
array( PCLZIP_ATT_FILE_NAME => $string_file,
PCLZIP_ATT_FILE_CONTENT => $prop
)
)
);
if ($b_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
}
So - anyone knows how to OVERWRITE a file from string (and not from file..) with PCLzip ??