1

I'm running into an error while importing multiple products with the same image name in one transaction.

The file "/var/www/magento/pub/media/tmp/catalog/product/t/e/test.jpg" doesn't exist or not a file

=> FileSystemException in vendor/magento/framework/Filesystem/Directory/Write.php line 70:

Importing every single product in one transaction seems to work fine (duplicates the image file), but is slow for importing 50.000 - 100.000 products.


Code Snippet:

$transactionFactory = $objectManager->get('\Magento\Framework\DB\TransactionFactory');
$transaction = $transactionFactory->create();

foreach ($xml->product as $productXml) {
    $mageProduct = $productFactory->create();
    $mageProduct->setSku(...);
    $mageProduct->setName(...);
    ...

    foreach ($productXml->images as $image) {
        // /var/www/magento/pub/media/import/product/IMAGE.jpg
        if (file_exists($checkPath)) {
            try {
                $imgAttributes = [];
                if ($imageCnt === 1) {
                    $imgAttributes = ['image', 'small_image', 'thumbnail', 'swatch_image'];
                }

                // $mediaPath = import/product/IMAGE.jpg
                $mageProduct->addImageToMediaGallery($mediaPath, $imgAttributes, false, false);
                ++$imageCnt;
            }
        }
    }

    $mageProduct->setIsMassupdate(true);
    $transaction->addObject($mageProduct);
}

$transaction->save();

Example data in xml file:

  • Product 1 - SKU ABC
    • Imagename 1: abc.jpg
    • Imagename 2: test.jpg
  • Product 2 - SKU DEF
    • Imagename 1: def.jpg
    • Imagename 2: test.jpg => causing the error

While saving the first image Magento creates a tmp file and removes that file after its finished. But for the next product it doesn't create a tmp file?

Is there anything wrong with my addImageToMediaGallery call?

  • Strange indeed, since you call addImageToMediaGallery with $move set to False. If you set back $exclude to True (its default value), what happens? – nicolallias Dec 18 '18 at 16:12
  • Thanks @nicolallias for your advice :-) I tried that before to check what $move is actually doing.. but setting $move to true will move the image file from it's givin source directory to the magento default product image directory (pub/media/catalog/product/..) so for the next product with the same image there is no image file in the source dir anymore. – Sebastian Dec 19 '18 at 13:30
  • 1
    i mean..the workaround is simple. track duplicate files and copy them in source dir with different name.. but that doesnt seem legit to me? :/ maybe there is a better way – Sebastian Dec 19 '18 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.