Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I uploaded product image programatically by using following code.

$files = $_FILES['images']['name'];
$f = substr($files,0,1);
$s= substr($files,1,1);
$absolute_path = Mage::getBaseDir('media').DS.'catalog'.DS.'product'.DS.$f.DS.$s; 
if(isset($files) && $files != '') 
               $var = rand(0,99); 
               $files = $var.'_'.$files; 
          // Starting upload 
          $uploader = new Varien_File_Uploader('images'); 
          //Here 4 extention would work 
          //false -> get the file directly in the specified folder 
          //true -> get the file in the product like folders /media/catalog/product/file.gif 
          //We set media as the upload dir 
          $uploader->save($absolute_path, $files);

    catch(Exception $e) 

$absolute_path = Mage::getBaseDir('media').DS.'catalog'.DS.'product'.DS.$f.DS.$s.DS.$files;

$mediaArray = array(
        'thumbnail'   => $absolute_path,
        'small_image' => $absolute_path,
        'image'       => $absolute_path,
    foreach($mediaArray as $imageType => $files)
        $filePath = $files;
             $product->addImageToMediaGallery($filePath, $imageType, false);
            catch (Exception $e)
                echo $e->getMessage();
            echo "Product does not have an image or the path is incorrect. Path was: {$filePath}<br/>";

It is added in backend but in the table


uploaded images disabled column value is 1.

If I upload image from backend, the uploaded image disabled column value is set as 0. How to fix this.

share|improve this question
up vote 3 down vote accepted

The addImageToMediaGallery method accepts a fourth argument (exclude - a boolean) that sets the image to enabled or disabled. It defaults to true, which means exclude from the gallery/disabled.

Change your code to

$product->addImageToMediaGallery($filePath, $imageType, false, false);

Also, I don't think you need to loop through $mediaArray to create each size you can call the method once like

    array('thumbnail', 'small_image', 'image'), 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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