opencart自定义下载错误

时间:2017-12-11 08:58:05

标签: php download opencart

我的自定义下载按钮已停止工作, 如果我点击下载按钮,它应该下载一个zip文件

downloadZip的功能是

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

它正在向我展示

  

PK { ^ [ e 0t 8 %1 R L$ ( t3 8 8 ݎo W (j 3) (] X]   F9" To1Zڈ8 $恩`đ8XqM= zk8〜Kz4ϸfcq xWŬ8m] e4I9Ժ(MF֎qT的&安培; TM〕中号⿅'R。 ޮ “{T $ XC ^ *”} _ C / - ?])胆红W¯¯-T (/ \ SZ; FUO〜&GT;〜ߵH〜)&LT;〜4瓦特{$ O02B l77Fy.PKޜHQF〜2%正܋ķ}ҋۑ4   &安培; + CGP) '@ =)' 户Q = HUV ^] =&GT;   “Z =#他:iVRm_ƺ8" G * E;,:斧,Z
  ü^ $ NA +UӰT])&LT;ٻ= PK

这可能是什么原因?

1 个答案:

答案 0 :(得分:1)

您已将HTTP标头内容类型设置为CSV:

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

你应该使用这样的东西:

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