如何从magento前端上传产品图片 [英] How to upload image of product from front end in magento
本文介绍了如何从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?
推荐答案
首先在media / import中上传图片
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();
这篇关于如何从magento前端上传产品图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文