Navin Parmar
Navin Parmar

Reputation: 167

How to upload image of product from front end in magento

I'm trying to upload the image of the product in admin panel. It's working fine but now I want to upload the image of the product in front end.

I mean customer can upload the image of the product from the front end. So how this is possible?

Upvotes: 7

Views: 1574

Answers (2)

Laxman Singh
Laxman Singh

Reputation: 391

First upload image in media/import

if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {
    $fileName       = $_FILES['file']['name'];
    $fileExt        = strtolower(substr(strrchr($fileName, "."), 1));
    $fileNamewoe    = rtrim($fileName, $fileExt);
    $fileName       = str_replace(' ', '', $fileNamewoe) . $fileExt;

    $uploader       = new Varien_File_Uploader('file');
    $uploader->setAllowedExtensions(array('png', 'jpg', 'jpeg')); //allowed extensions
    $uploader->setAllowRenameFiles(false);
    $uploader->setFilesDispersion(false);
    $path = Mage::getBaseDir('media') . DS . 'import';
    if(!is_dir($path)){
        mkdir($path, 0777, true);
    }
    $uploader->save($path . DS, $fileName );
}

Now save product with uploaded image

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load($id);
$product->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
$imagePath = Mage::getBaseDir('media') . DS . 'import/'. $fileName;
$product->addImageToMediaGallery($imagePath,array('image', 'small_image', 'thumbnail'),true,false);
$product->save();

Upvotes: 4

Aleksey Razbakov
Aleksey Razbakov

Reputation: 634

One of the most enticipated and needed things in Magento is File upload Custom Option. As discussed last year at Magento Forum, it is not completed nor tested.

Now, Magento already have frontend and admin part of file upload option implemented in themes. Since backend part is still missing, understand that this still doesn’t work, however, if you’re interested how it looks, read on

http://inchoo.net/magento/file-upload-in-magento/

Upvotes: 1

Related Questions