Magento – Magento:Save Product image from external url

magento-1.9productprogrammaticallysave

I want to save a simple product programatically.I have used the following link http://inchoo.net/magento/programming-magento/programatically-manually-creating-simple-magento-product/

All fields are saved except image. The image url is from an external link as (for example) http://test12.net/test/public/uploads/shanks/shank-for-test-2.png

I want to copy the image and save as product image .Please help.

Best Answer

Enter code here`Please follow below steps:-

  1. Create one "imgs2.csv" with the column name of "IMAGE" (assign all external images URLs) put the same on Magento root directory.

  2. Create one new directory "uploads" on Magento root directory.

  3. Create a file with name "download_img.php" on Magento root directory and following code in the same file, after you hit the same file on website URLs for example http://www.example.com/download_img.php

    $csv = array_map('str_getcsv', file($file));
    //print_r($csv);
    array_walk($csv, function(&$a) use ($csv) {
      $a = array_combine($csv[0], $a);
    });
    array_shift($csv); # remove column header
    //print_r($csv);
    
    
    foreach($csv as $key=>$img){
        //echo $img['IMAGE'];
         $import_location = 'uploads';
        $file_target = $import_location."/".basename($img['IMAGE']);
    
        $image = file_get_contents($img['IMAGE']);
    file_put_contents($file_target,$image);  
    
        /* if(!empty($img)){
            $my_image = file_get_contents($img);
            file_get_contents(filename)
    
            $filename = $data[0] . ".jpg";
             echo $filename. "\n";
            if(!file_exists($filename))
                {
                    $my_file = fopen($filename,'w+');
                    file_put_contents($filename, $my_image);
                    echo $filename . "\n";
    
                }
        }
            $image = "/uploads/" . $filename;
            echo $image . "\n"; */
    }
    
    ?>
    
Related Topic