How to upload image of product from front end in magento

假装没事ソ 提交于 2019-12-23 09:02:17

问题


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?


回答1:


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();



回答2:


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/



来源:https://stackoverflow.com/questions/29530130/how-to-upload-image-of-product-from-front-end-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!