Magento – Uploading images while uploading product programmatically on frontend

imagemagento-1.7

In my project, customer can upload their products from the user dashboard ( not from super admin dashboard). I have made a product upload form where customer can upload their product or items. I am able to save all the product information through custom form. However, I am unable to upload the product images. I have gone through this post

https://stackoverflow.com/questions/8456954/magento-programmatically-add-product-image unable to uploade it

I have tired the code as after saving product information thru form :

$filename = $_FILES['image']['name'];

        $mediaArray = array(
            'thumbnail'   => $filename,
            'small_image' => $filename,
            'image'       => $filename,
        );

        // Remove unset images, add image to gallery if exists
        $importDir = Mage::getBaseDir('media') . DS . 'import'.DS;

        foreach($mediaArray as $imageType => $fileName) {
        echo    $filePath = $importDir.$fileName;

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

          }
     }

It shows the error as :

There has been an error processing your request. Image does not exist.

Best Answer

There's likely a small issue with the file path somewhere in there. I know that you said you echo'd it out, but you may be missing something like perhaps a leading slash that shouldn't be there, etc.

One little thing I like to do to be 100% sure when inspecting this kind of thing, is to copy the file path, open up terminal, go to the Magento root, then ls <pasted_value> - that way you know for sure if the path is right or not.

The error message that you're seeing is originating in:

Mage_Catalog_Model_Product_Attribute_Backend_Media::addImage()
{
    ...
    if (!$file || !file_exists($file)) {
        Mage::throwException(Mage::helper('catalog')->__('Image does not exist.'));
    }
Related Topic