1

i want to upload image from url to magento media path using object manager i have created a code for it the url image is saved to my media folder but not assigning to product it returns that image doesn't exist but the file was uploaded to my temp_upload folder

Any suggestion would be helpful

public function addImageFromUrl($urlToImage)
 {
 $objectManager = $bootstrap->getObjectManager();
 $state = $objectManager->get('Magento\Framework\App\State');
 $state->setAreaCode('frontend');
 $directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
 $mySaveDir = $directory->getRoot()."/pub/media/temp_upload/";
 $filename = basename($urlToImage);
 $completeSaveLoc = $mySaveDir.$filename;
 if(!file_exists($completeSaveLoc)){
 try {
 file_put_contents($completeSaveLoc,file_get_contents($urlToImage));
 return $completeSaveLoc;
 }
 catch (Exception $e){
 echo $e->getMessage();
 }
 }
}
$imageUrlPath = "https://www.festisite.com/static/partylogo/img/logos/google.png";
if(!empty($imageUrlPath))
 {
 $returnUploadedImagePath = $this->addImageFromUrl($imageUrlPath);
 $_product->addImageToMediaGallery($returnUploadedImagePath, array('image','thumbnail','small_image'), false); 
 }
asked Nov 28, 2017 at 5:44
1
  • have you fine solution? Commented Feb 5, 2019 at 13:47

1 Answer 1

1

You have to move your file from

/pub/media/temp_upload/

directory to

pub/media/catalog/product

using core PHP function move_uploaded_file ( string $filename , string $destination )

Either you can use Magento's

$this->getImageUploader()->moveFileFromTmp($image);

And after that return Media path.

answered Nov 28, 2017 at 6:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.