5

i created php file in root folder and trying to create simple product programatically in magento 2. testimg folder locating in root. I always thow the error: The image does not exist. Here is code:

$_product->setImage('testimg/test.jpg');
$_product->setSmallImage('testimg/test.jpg');
$_product->setThumbnail('testimg/test.jpg');
$_product->addImageToMediaGallery(
    'testimg/test.jpg',
    null,
    false,
    false
);
$_product->addImageToMediaGallery(
    'testimg/test.jpg',
    null,
    false,
    false
);

when i put 'testimg' folder in 'pub/media' and run script:

$dir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList');
$_product->addImageToMediaGallery(
    $dir->getPath('media') . '/testimg/test.jpg',
    null,
    false,
    false
);

i receive next error: The file "/home/xxx/pub/media/tmp/catalog/product/t/e/test.jpg" doesn't exist or not a file. Why magento looking for my file in tmp/catalog/product/t/e/ ?

4

In Magento 2, the root folder is not by default accessible. You need to put your testimg folder in pub/media to make it work. Move your folder to pub/media and update your code like below.

Here you are using an external php file, so you can do it like below.

$obj = $bootstrap->getObjectManager();
$dir = $obj->get('Magento\Framework\App\Filesystem\DirectoryList');

$_product->addImageToMediaGallery(
    $dir->getPath('media').'/testimg/test.jpg',
    null,
    false,
    false
);
$_product->save();
  • i put the folder in pub/media folder but receive this error: The file "/home/xxx/pub/media/tmp/catalog/product/t/e/test.jpg" doesn't exist or not a file in /home/xxx/vendor/magento/framework/Filesystem/Directory/Write.php:69 – John Sep 4 '17 at 18:46
  • @john, can you share your updated code to question? – Jaimin Sutariya Sep 4 '17 at 21:45
  • ........updated – John Sep 5 '17 at 7:34
  • Do you have assigned 777 permission for pub/media folder? File upload functionality works that way, first it copies the file in a tmp folder and then move it to specified location. That's why Magento tries to get file from tmp folder location. Please assign permission and check. @john – Jaimin Sutariya Sep 5 '17 at 8:03
  • yes i just set 777 perms for pub/media folder and still same error but folders '/pub/media/tmp/catalog/product/t/e/' was created but there is no my picture file – John Sep 5 '17 at 10:14
1

'testimg/test.jpg'should be put absolute path.

Suppose

If test.jpg exit at /home/XX/public_html/media/testimg then

You should change:

$_product->addImageToMediaGallery(
    'testimg/test.jpg',
    null,
    false,
    false
);

to

$_product->addImageToMediaGallery(
    '/home/XX/public_html/media/testimg/test.jpg',
    null,
    false,
    false
);
  • thanks for reply, unfortunately that's not helping – John Sep 4 '17 at 17:28

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.