hehe
hehe

Reputation: 75

How to prevent duplicate import image?

i am sorry i am a new learner of php.the following code is Allows you to import multiple images for each product.Simply add a 'gallery' column to the import file, and separate each image with a semi-colon.import data from the csv file. but there is a bug. if i have imported the image,when i import again.it still import again.namyly, the product have many duplicate images.if the product have imported image, how to prevent it importing again.

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) {}   

thank you.

Upvotes: 0

Views: 772

Answers (1)

Samuel Cook
Samuel Cook

Reputation: 16828

check to see if the file_exists(). if not import it:

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) {}

Note: This will dramatically, slow the process down as it has to reach out to the server each time to check for the file.

Upvotes: 1

Related Questions