I want to upload image by programmatically.

I have to imagefolder and i put it media/import/imagefolder. and My code is

require_once 'app/Mage.php';

 ini_set('display_errors', 1);
 ini_set('max_execution_time', 0);
 ini_set('memory_limit', '50024M');
$attrId = Mage::getSingleton('catalog/product')->getResource()->getAttribute('media_gallery')
$path = Mage::getBaseDir('media').'\import'.DS;
echo $path;
$data = array();
echo "<br>";
echo "<pre>";
$files = glob($path.'*.jpg' ,  GLOB_BRACE);
//$path_parts = explode(".",$files);
    foreach($files as $key=>$value)   
        $filename = basename($value);
        //echo $filename;
        $path_parts = explode(".",$filename);


        $data[] = array(
        'sku' => $path_parts[0],
        '_type' => 'simple',
        'status' => 1,
        'visibility' => 4,
        '_media_image'=> $value,
        '_media_is_disabled' =>0,
        'image'=> $filename,
        'small_image'=> $filename,
        'thumbnail'=> $filename,

//$data = array_values($data);

    } print_r($data);
    try {
    /** @var $import AvS_FastSimpleImport_Model_Import */
    $import = Mage::getModel('fastsimpleimport/import');
} catch (Exception $e) {

echo "Check Now";


but Image not uploaded.

so how can i do?

  • Is this on windows or linux? Also, is it finding any images at all? Are you getting any errors? – Sander Mangel Sep 8 '15 at 12:13
  • this is on windows and no one get error – ABC Sep 8 '15 at 12:26
  • Try to debug a little, does it find any images, is the image path valid etc. We have very little to go on – Sander Mangel Sep 8 '15 at 12:31

You need to use DS as directory spectator. not \

If you have using this then it must create issue in window & etc os.

$path = Mage::getBaseDir('media').'\import'.DS;

should be

$path = Mage::getBaseDir('media').DS.'import'.DS;

Mage::getBaseDir('media') =YourMagentoInstacePath/mediaFolder

| improve this answer | |

There is a minor mistake and I changed this

$path = Mage::getBaseDir('media').'\import'.DS;


$path = Mage::getBaseUrl('media').'\import'.DS;
| improve this answer | |
  • Mage::getBaseUrl('media') give the media url not media path...your answer is wrong – Amit Bera Sep 8 '15 at 16: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.