我需要一个简单的函数,将一组文件写入一个 zip 文件。我从在线教程中找到了一些代码并稍作修改,但我似乎无法让它工作。它会创建 zip 文件,但是当我尝试提取它时,我得到一个错误:
Windows cannot complete the extraction.
The Compressed (zipped) Folder '...' is invalid.
这是我正在使用的代码:
public 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 'file exists'; }
$valid_files = array();
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
Zend_Debug::dump($valid_files);
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination, ZIPARCHIVE::CREATE) !== true) {
return 'could not open zip: '.$destination;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file);
}
//debug
Zend_Debug::dump($zip->numFiles);
Zend_Debug::dump($zip->status);
$zip->close();
//check to make sure the file exists
return file_exists($destination);
} else {
return 'no valid failes'. count($valid_files);
}
}
调试语句打印出以下内容:
For $valid_files - array of one file name (full path to file)
For $zip->numFiles - 1
For $zip->status - 0
The function returns true.
关于我做错了什么的任何想法?