$mediaAttribute = array (‘thumbnail’,’small_image’,’image’);
$mediaDir = $objectManager->get(‘Magento\Framework\App\Filesystem\DirectoryList’)->getPath(‘media’);// Im Magento 2
//$mediaDir = Mage::getBaseDir(‘media’);// In Magento 1
$fpath = ‘product/images/’;// path to your file
$count = 0;
$imgArray = array($relatantimgeurl);
$tempcatimg = $mediaDir.’/tempcatimg’;
foreach ($imgArray as $image){
save_image( $image,$objectManager,$tempcatimg );
// copies the file from your local storage to your-magento-root-path/pub/media

//save images here end
echo $count.’img uploaded’;

function save_image($img,$objectManager,$mediaDir) {
$imageFilename = basename($img);
$image_type = substr(strrchr($imageFilename,”.”),1); //find the image extension
echo $filename = md5($img . strtotime(‘now’)).’.’.$image_type; //give a new name, you can modify as per your requirement
echo ‘<br/>’;
/* if (!file_exists($mediaDir)) mkdir($mediaDir, 0777, true);
else chmod($mediaDir, 0777); */
$filepath = $mediaDir . ‘/’. $filename; //path for temp storage folder: pub/media
//$data = file_get_contents(trim($img));
//Store in the filesystem.
$fp = fopen($filepath, “w”);
fwrite($fp, $ggh);
// file_put_contents($filepath, file_get_contents(trim($img))); //store the image from external url to the temp storage folder
return $filepath;


Please login or Register to submit your answer

Log In

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