0

I am importing images through products import. But I am getting an error.

"1. Imported resource (image) could not be downloaded from external resource due to timeout or access permissions in a row(s): 2, 3, 4, 5, 6, 7, 8".

My images folder is pub/media/import.

How to overcome this?

1 Answer 1

1

Use the following code to import product images magento 2

$mainImage = $importProduct[9];

        if ($mainImage) {
            $image_directory = $mediaPath . DS . 'data' . DS . $mainImage;
            if (file_exists($image_directory)) {
                $product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
                    ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);//assigning image, thumb and small image to media gallery
            } 
            else {
                $image_directory = $mediaPath . 'data' . DS . 'comingsoon.jpg';
                $product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
                    ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);
            }
        }

        // additional images
        if ($importProduct[29] != '') {
            $addImages = explode(",", trim($importProduct[29]));
            foreach ($addImages as $additional_image) {
                $image_directory = $mediaPath .DS.'data'.DS. trim($additional_image);
                if (file_exists($image_directory)) {
                    $product->addImageToMediaGallery($image_directory, null, false, false);
                } 
                else {
                    $image_directory = $mediaPath . 'data' . DS . 'comingsoon.jpg';
                    $product->addImageToMediaGallery($image_directory, null, false, false);
                }
            }
            echo 'Additional images for product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
        }

Please click here for detailed explanation. http://www.pearlbells.co.uk/code-snippets/import-product-images-magento-2-programmatically/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.