如何在opencart的商品页面前端添加一个按钮来下载所有商品图片?

发布于 2025-01-15 16:39:17 字数 1967 浏览 5 评论 0原文

我得到了这个代码,但它不起作用.. 此代码的问题是它下载一个空的 zip 文件...

我想下载所有产品图像(zip 文件或下载而不压缩)

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);
            }

I got this code but it doesn't work..
The problem with this code is that it downloads an empty zip file ...

I want to download all product images (either zip file or download them without compression)

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);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2025-01-22 16:39:17

当我测试下面的代码时,它正在下载带有文件夹的图像。
因此,我相信您的临时文件夹创建然后添加到 zip 文件夹会产生问题。

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'];
    }

    $zipname = $this->request->get['product_id'] . '.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename=' . $zipname);
    header('Content-Length: ' . filesize($zipname));
    readfile($zipname);

}

电子商务下载

When I tested the code below it is downloading the images with folders.
So, I believe your temp folder creation and then adding to the zip folder are creating issues.

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'];
    }

    $zipname = $this->request->get['product_id'] . '.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename=' . $zipname);
    header('Content-Length: ' . filesize($zipname));
    readfile($zipname);

}

eCommerce download

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文