0

对不起,我是 php 的新手。下面的代码是允许您为每个产品导入多个图像。只需在导入文件中添加一个“图库”列,并用分号分隔每个图像。导入数据来自.csv 文件。但有一个错误。如果我已经导入了图像,当我再次导入时,它仍然会再次导入。当然,产品有很多重复的图像。如果产品已经导入了图像,如何防止它再次导入。

try {
                    $galleryData = explode(';',$importData["gallery"]);
                    foreach($galleryData as $gallery_img)
                    /**
                     * @param directory where import image resides
                     * @param leave 'null' so that it isn't imported as thumbnail, base, or small
                     * @param false = the image is copied, not moved from the import directory to it's new location
                     * @param false = not excluded from the front end gallery
                     */
                    {
                            $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false);
                    }
                }
            catch (Exception $e) {}   

谢谢你。

4

1 回答 1

1

检查是否file_exists()。如果不导入:

try {
    $galleryData = explode(';',$importData["gallery"]);
    foreach($galleryData as $gallery_img){

        if(!file_exists(Mage::getBaseDir('media') . DS . 'import' . $gallery_img)){
            $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false);
        }

    }
}catch (Exception $e) {}

注意:这将大大减慢进程,因为它每次都必须与服务器联系以检查文件。

于 2012-12-28T02:01:02.333 回答