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);
}
-
have you fine solution?Bhagyashri Pawar– Bhagyashri Pawar2019年02月05日 13:47:55 +00:00Commented Feb 5, 2019 at 13:47
1 Answer 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.