0

When i'm trying to set image for the product

$configurable->setMediaGallery(array('images'=>array (), 'values'=>array ()));
$path = $this->helper->uploadImage($data->main_image);
if ($path && file_exists($path)) {
    $configurable->addImageToMediaGallery($path, array('image', 'small_image', 'thumbnail'), true, false);
}

and then when save it i'm getting an error

Exception #0 (Magento\Framework\Exception\FileSystemException): The file "... pub/media/tmp/catalog/product/8/1/81cz5-izU0L.jpg" doesn't exist or not a file

Looks like some permission issue but i've checked all directories have all needed access. Any suggestions? Thanks.

1

The file path should be relative to media folder. Below I'll try to explain why.

So addImageToMediaGallery calls Model\Product\Gallery\Processor.php::addImage

 $file = $this->mediaDirectory->getRelativePath($file);
 if (!$this->mediaDirectory->isFile($file)) {
      throw new LocalizedException(__('The image does not exist.'));
 }

You can do a debug of the $file, and then keep digging which will eventually bring you to Magento\Framework\Filesystem\Directory\Read

Particularly this line 219: $this->driver->getAbsolutePath($this->path, $path)

Now, if your path was "/fullpath/pub/media/../file.jpg" which you passed to addImageToMediaGallery, then by debugging the result of getAbsolutePath call, you will get something like this:

/fullpath/pub/media/fullpath/pub/media/../file.jpg

So your path is prefixed with the full server path to your media folder and that's make it invalid and therefore exception 'The image does not exist.' is thrown.

Hope it makes it more clear now.

0

I also got the same error when I was trying to save the additional images as part of the main import. So I set the main as part of the first import and set the additional images after that using the following code. That works for me.

 $imgs = explode('|',$prodData[10]);

        if(sizeof($imgs) > 1) {

            $product = $_objectManager->create('Magento\Catalog\Model\Product')->load($newProdId);
            $productRepository = $_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
            $productRepository->save($product);

            for ( $i=1; $i<sizeof($imgs); $i++ ) {

                echo 'Add Images :' . $prdbasepath.basename(trim($imgs[$i])) . PHP_EOL;
                $image_directory = $prdbasepath.'data'.DS.basename(trim($imgs[$i]));


                if (file_exists($image_directory) && getimagesize($image_directory)) {

                    echo 'File exists'.PHP_EOL;
                    $product->addImageToMediaGallery($image_directory, array('image', 'small_image', 'thumbnail'), false, false);
                    $product->save();

                }
            }

        }

Your Answer

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

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