点击magento中的下载按钮,以压缩方式下载产品图片

时间:2015-09-30 04:09:40

标签: php magento

我有一个类别特定产品的自定义页面。

这是我仅显示所有图像的链接和代码。

<div style="float:left; width:100%;">
    <?php $_helper = $this->helper('catalog/output'); ?>
    <?php $product = $this->getProduct(); ?>
    <?php foreach ($product->getMediaGalleryImages() as $image) :?>

        <div style="width: 48%; margin:1%;float:left">
            <img width="100%;" src="<?php echo Mage::helper('catalog/image')->init($product, 'image', $image->getFile()); ?>" alt="<?php echo $product->getName()?>" />
        </div>

    <?php endforeach; ?>

</div>
<div style="float:left; width:100%;">
    <button type="button" style="display:block;margin:0 auto;">Download</button>
</div>

现在我需要点击按钮下载产品的所有图像,链接是 http://thevastrafashion.com/english/testing-1.html 我怎样才能做到这一点。如果您还想要更多信息,请发表评论。

1 个答案:

答案 0 :(得分:2)

您可以执行此操作。

foreach ($sku as $key => $value) {
    $productId = $value;
    $products = Mage::getModel('catalog/product')->loadByAttribute('sku', $productId);
    $inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($products)->getIsInStock();
    if($inStock)
    {
        array_push($file_arr, (string) Mage::helper('catalog/image')->init($products, 'image'));
    }
}
if(count($file_arr)>0)
{
    $result = create_zip($file_arr);
}

function create_zip($files = array()) {
    try{
    $zip = new ZipArchive();
    $tmp_file = tempnam('.', '');
    $zip->open($tmp_file, ZipArchive::CREATE);
    foreach ($files as $file) {
        $download_file = file_get_contents($file);
        $zip->addFromString(basename($file), $download_file);
    }
    $zip->close();
    header('Content-disposition: attachment; filename=download.zip');
    header('Content-type: application/zip');
    header("Accept-Ranges: bytes");
    header("Pragma: public");
    header("Expires: -1");
    header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0"); 
    header('Content-Length: ' . filesize($tmp_file));
    $chunksize = 8 * (1024 * 1024); //8MB (highest possible fread length)
          $handle = fopen($tmp_file, 'rb');
          $buffer = '';
          while (!feof($handle) && (connection_status() === CONNECTION_NORMAL)) 
          {
            $buffer = fread($handle, $chunksize);
            print $buffer;
            ob_flush();
            flush();
          }
          if(connection_status() !== CONNECTION_NORMAL)
          {
            echo "Connection aborted";
          }
          fclose($handle);
    }
    catch(Exception $e)
    {
        Mage::log($e,null,'download_error.log');
    }
}

这只是参考代码。所以你需要根据你的要求进行改变。