Pankti Shah
Pankti Shah

Reputation: 387

opencart custom download error

My custom download button has stopped working, If I click on download button it should download one zip file

The function for downloadZip is

    public function downloadZip()
    {
        $products=$this->session->data['products'];
        $attributes=$this->session->data['attributes'];
        /* -------------------  CSV ------------------------*/
        header("Pragma: no-cache");
        header('Content-Type: text/csv;');
        $csv_filename = "image/".$this->request->get['catalogno'].".csv";
        $output = fopen($csv_filename, "w"); //Opens and clears the contents of file; or creates a new file if it doesn't exist
        $i=0;
        $temp_data[$i][]='Model';
        $temp_data[$i][]='Price';
        foreach ($attributes as $attribute)
        {
            $temp_data[$i][]=$attribute['name'];
        }
        $i++;
        foreach ($products as $key => $row)
        {
            $mid[$key]  = $row['model'];
        }
        array_multisort($mid, SORT_ASC, $products);
        foreach ($products as $product)
        {
            $temp_data[$i][]=$product['model'];
            $temp_data[$i][]=$product['pprice'];
            if(!empty($product['attribute_groups']))
            {
                foreach ($product['attribute_groups'] as $attribute_group)
                {
                    foreach ($attribute_group['attribute'] as $attribute)
                    {
                        foreach ($attributes as $attr)
                        {
                            if($attr['name'] == $attribute['name'])
                            {
                                $temp_data[$i][]=$attribute['text'];
                            }
                        }
                    }
                }
            }
            else
            {
                foreach ($attributes as $attribute)
                {
                    $temp_data[$i][]=' - ';
                }
            }
            $i++;
        }
        // Exporting the CSV
        foreach($temp_data as $row)
        {
            fputcsv($output, $row); // here you can change delimiter/enclosure
        }
        //fclose($output); // Closing the File
        /*------------------- End CSV --------------------*/
        /*------------------- Start HTML --------------------*/
        $f = fopen("image/Catalog".$this->request->get['catalogno'].".html", "w");
        $details='';
                    foreach ($products as $key => $row)
                    {
                        $mid[$key]  = $row['model'];
                    }
                    array_multisort($mid, SORT_ASC, $products);
                    $details.="<table>
                        <tr>
                            <td colspan='".(count($attributes)+2)."' align='center'><h1>Catalog</h1></td>
                        </tr>
                        <tr>
                            <th>Model</th>
                            <th>Price</th>";
                            foreach ($attributes as $attribute)
                                {
                                    $details.="<th>".$attribute['name']."</th>";
                             }
                        $details.="</tr>";
                   foreach ($products as $product) {
                      if($product['sspecial'])
                      {
                            $rate=$product['sspecial'];
                      }
                      else
                      {
                            $rate=$product['pprice'];
                      }
                        $details.="<tr>
                            <td>".$product['model']."</td>
                            <td>".$rate."</td>";
                if(!empty($product['attribute_groups']))
                    {
                        foreach ($product['attribute_groups'] as $attribute_group)
                        {
                         foreach ($attribute_group['attribute'] as $attribute)
                            {
                                foreach ($attributes as $attr)
                                {
                                    if($attr['name'] == $attribute['name'])
                                    {
                                        $details.="<td>".$attribute['text']."</td>";
                                    }
                                }
                           }
                       }
                   }
                   else
                   {
                        foreach ($attributes as $attribute) {
                            $details.="<td> - </td>";
                        }
                   }
               }
               $details.="</tr>
               </table>";
    $details .="<style>
table
{
    width:100%;
}
table tr td,table tr th {
    border:1px dashed #999;
    padding:5px 10px;
}
</style>";
        // Write text line
        fwrite($f, $details);
        // Close the text file
        fclose($f);
        /*------------------- END HTML --------------------*/
        unset($this->session->data['products']);
        unset($this->session->data['attributes']);
        $this->load->model('catalog/product');
        $this->load->model('tool/image');
        $productData = $this->model_catalog_product->getProductAllImages($this->request->get['catalogno']);
        //$result = $this->create_zip($productData,'my-archive.zip');
        $zip = new ZipArchive;
        $zip_name = "image/Catalog".$this->request->get['catalogno'].".zip";
        if ($zip->open($zip_name,  ZipArchive::CREATE)) {
            foreach($productData as $key=>$image)
            {
                $filename = substr(strrchr($image['image'], "/"), 1);
                $zip->addFile("image/".$image['image'], $filename);
            }
            $zip->addFile("image/Catalog".$this->request->get['catalogno'].".html","Catalog".$this->request->get['catalogno'].".html");
            $zip->addFile("image/".$this->request->get['catalogno'].".csv",$this->request->get['catalogno'].".csv");
            $zip->close();
            header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length: ".filesize($zip_name));
            header("Content-Disposition: attachment; filename=\"".basename($zip_name)."\"");
            readfile($zip_name);
            unlink($zip_name);
            unlink("image/Catalog".$this->request->get['catalogno'].".html");
            unlink($csv_filename);
        } else {
            return false;
        }
        $this->index();
    }

It is showing me

PK�{���^��[�e��0t���8�%1�R�L$�(���t 3�8�8�ݎo�W�(j� 3��(]X�] ��F�9�"�����To1�Z�ڈ�����8$En�������*`đ8�XqM=zk�8�~Kz��4ϸf�cq��x�WŬ��8m���]]�e�4��I9Ժ(�M���F���֎.�q��T&�T�m]�M�⿅�'r.�ޮ"{�T$XC�^؞"}�?�����_C/�-])�����s��B���?��w��-���T��(/�\s���z���;Fu�o~�>~��ߵh�~��)<~4����w�{�$O�0��2B��l�7����7�F�y.PK�ޜ�h�qF~2���%��n܋�K������}ҋ���ۑ4 �&+����CgP,)'@����=)'�pQ�=HU��v^]=>�� �'z=�#٫H��e:iVR��m_ƺ8�"��G��*e��;�,��:��a����x,z
u^$ńA�+?U�ӰT�]��)��<ٻ=�PK

What might be the reason for this?

Upvotes: 0

Views: 95

Answers (1)

Paul Feakins
Paul Feakins

Reputation: 727

You've set the HTTP header content type to CSV:

header('Content-Type: text/csv;');

You should instead use something like this:

header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");

Upvotes: 2

Related Questions