我正在努力创建与ZipArchive的邮编。我修改了一个函数,让我从一组文件路径创建一个zip文件。ZipArchive - 邮编没有被创建,但不会引发错误
您是否可以发现此代码中的任何错误,防止创建zip文件的任何错误?
// Function to create a zip archive for the low res images
function create_zip_lres($files = array(), $destination = '', $overwrite = false) {
// If the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
// If files were passed in
if(is_array($files)) {
foreach($files as $file) {
// Make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
// If we have good files
if(count($valid_files)) {
// Create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
// Add the files
foreach($valid_files as $file) {
$basename = basename($file);
$zip->addFile($file, $basename);
}
// DEBUG
echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
if ($zip->close()) {
echo ' - Success!';
echo '<br>';
echo 'Your zip path is: ' . $destination;
} else {
echo ' - Failed!';
}
// DEBUG
echo '<br>';
print_r($zip);
// Return filepath for zip
return $destination;
}
else {
return false;
}
}
// getting the images array
$resize_images = resize_images();
// destination path
$lres_directory = get_lres_full_upload_dir() . get_the_title() . '-low-res.zip';
if (function_exists('create_zip_lres')) {
create_zip_lres($resize_images, $lres_directory);
} else {
}
我已经添加了一些调试线,找出发生了什么,尽管它告诉我一切正常,不会创建任何文件。
The zip archive contains 3 files with a status of 0 - Success!
Your zip path is: http://media.mysite.com/wp-content/uploads/lres-download-manager-files/Knus 1400-low-res.zip
ZipArchive Object ([status] => 0 [statusSys] => 0 [numFiles] => 0 [filename] => [comment] =>)
要压缩特定文件夹? – RaMeSh 2015-03-31 10:37:59
文件名中的空格? – pokeybit 2015-03-31 10:41:01
@用户我将多个文件压缩到一个zip文件,但是zip文件没有被创建。 – michaelw90 2015-03-31 11:22:27