I'm trying to create a zip file of user uploaded images that will be generated for download when a user clicks a button. The button is on a page in the Wordpress admin. The page is from a plugin that adds a new admin section. When the button is clicked, it submits a form to a page in 'themes/twentyfourteen/page-templates' directory. The page is called cartoon_zip.php and was created through the Pages section in the admin, so it has the url of http://www.sitename.com/cartoon-zip/. When I click the file, it appears to work. No errors occur, and a save file dialog appears. I save the file locally and try to unzip but get the message 'The contents of the file "characters.zip" can not be extracted with this program.' I'm on a mac, so it's The Unarchiver trying to open; it doesn't work on a PC either though. Below is the code I'm using; any help is greatly appreciated.
<?php
/*
Template name: Cartoon Zip
*/
if(isset($_REQUEST['order_id'])){
$order_id = $_REQUEST['order_id'];
$query = "SELECT optional_image FROM order_listing WHERE order_no = '".$order_id."'";
$query2 = "SELECT main_image FROM character_order WHERE order_id = '".$order_id."'";
$entries = $wpdb->get_results($query);
$entries2 = $wpdb->get_results($query2);
$error = ""; //error holder
if(extension_loaded('zip')){ // Checking ZIP extension is available
$zip = new ZipArchive(); // Load zip library
$zip_name = "characters.zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach( $entries as $entry ) {
$image=$entry->optional_image;
if(!empty($image)):
$url = 'wp-content/themes/twentyfourteen/upload/main/'.$image;
if(file_exists($url)){
$zip->addFile($url,$image); // Adding files into zip
}
endif;
}
foreach( $entries2 as $entry ) {
$image=$entry->main_image;
if(!empty($image)):
$url = 'wp-content/themes/twentyfourteen/upload/character/'.$image;
if(file_exists($url)){
$zip->addFile($url,$image); // Adding files into zip
}
endif;
}
if ($zip->close() === false) {
exit("Error creating ZIP file");
};
$filename = $zip_name;
// send $filename to browser
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $filename);
$size = filesize($filename);
$name = basename($filename);
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
// cache settings for IE6 on HTTPS
header('Cache-Control: max-age=120');
header('Pragma: public');
} else {
header('Cache-Control: private, max-age=120, must-revalidate');
header("Pragma: no-cache");
}
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // long ago
header("Content-Type: $mimeType");
header('Content-Disposition: attachment; filename="' . $name . '";');
header("Accept-Ranges: bytes");
header('Content-Length: ' . filesize($filename));
readfile($fullFilePath);
exit;
}else
$error .= "* You dont have ZIP extension<br/>";
}
?>
EDIT: So I manged to get this working with a combination doublesharp's help below and by changing the headers to the following.
$file = $zip_name;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
Here's the full working code.
<?php
/*
Template name: Cartoon Zip
*/
if(isset($_REQUEST['order_id'])){
global $wpdb;
$order_id = isset( $_REQUEST['order_id'] )? $_REQUEST['order_id'] : 0;
$query = $wpdb->prepare( "SELECT optional_image FROM order_listing WHERE order_no = %s", $order_id );
$query2 = $wpdb->prepare( "SELECT main_image FROM character_order WHERE order_id = %s", $order_id );
$entries = $wpdb->get_results($query);
$entries2 = $wpdb->get_results($query2);
$error = ""; //error holder
if(extension_loaded('zip')){ // Checking ZIP extension is available
$zip = new ZipArchive(); // Load zip library
$zip_name = "characters.zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach( $entries as $entry ) {
$image=$entry->optional_image;
if( ! empty( $image ) ){
$filename = WP_CONTENT_DIR.'/themes/twentyfourteen/upload/main/'.$image;
if ( file_exists( $filename ) ){
// Adding files into zip
$zip->addFile( $filename, $image );
}
}
}
foreach( $entries2 as $entry ) {
$image=$entry->main_image;
if( ! empty( $image ) ){
$filename = WP_CONTENT_DIR.'/themes/twentyfourteen/upload/character/'.$image;
if ( file_exists( $filename ) ){
// Adding files into zip
$zip->addFile( $filename, $image );
}
}
}
if ($zip->close() === false) {
exit("Error creating ZIP file");
};
$file = $zip_name;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}else
$error .= "* You dont have ZIP extension<br/>";
}
?>