Opencart产品页面所有图片压缩为Zip文件供下载
Opencart Product Page All Image Compression to Zip File For Download
您好,我想在我的网站上展示一个图片库,然后有一个 link 允许用户下载整个图片库
- 所有图片自动转换为 zip 文件并在产品页面上显示按钮 "download image"
像这样:-
Product.php
创建新函数
public function downloadcatalog(){
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
foreach ($results as $result) {
$files[] = 'image/'.$result['image'];
}
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($files as $file) {
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
$file_name = $product_info['name'].'.zip';
# send the file to the browser as a download
/*header('Content-disposition: attachment; filename="'.$product_info['name'].'"');
header('Content-type: application/zip');*/
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($tmp_file));
//$zip->close();
//readfile($zipname);
readfile($tmp_file);
unlink($tmp_file);
}
============================================= =======================
Product.php
(Index控制器之前添加)
$data['column_left'] = $this->load->controller('common/column_left');
$data['download_catalog'] = $this->url->link('product/product/downloadcatalog', 'product_id=' . $this->request->get['product_id']);
您好,我想在我的网站上展示一个图片库,然后有一个 link 允许用户下载整个图片库
- 所有图片自动转换为 zip 文件并在产品页面上显示按钮 "download image"
像这样:-
Product.php
创建新函数
public function downloadcatalog(){
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
foreach ($results as $result) {
$files[] = 'image/'.$result['image'];
}
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($files as $file) {
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
$file_name = $product_info['name'].'.zip';
# send the file to the browser as a download
/*header('Content-disposition: attachment; filename="'.$product_info['name'].'"');
header('Content-type: application/zip');*/
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($tmp_file));
//$zip->close();
//readfile($zipname);
readfile($tmp_file);
unlink($tmp_file);
}
============================================= =======================
Product.php
(Index控制器之前添加)
$data['column_left'] = $this->load->controller('common/column_left');
$data['download_catalog'] = $this->url->link('product/product/downloadcatalog', 'product_id=' . $this->request->get['product_id']);