簡體   English   中英

如何在opencart的產品頁面前端添加一個按鈕來下載所有產品圖片?

[英]How can I add a button on the front end of the product page in opencart to download all product images?

我得到了這段代碼,但它不起作用。這段代碼的問題是它下載了一個空的 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);
            }

當我測試下面的代碼時,它正在下載帶有文件夾的圖像。 因此,我相信您的臨時文件夾創建然后添加到 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);

}

電子商務下載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM