点击这里 快速注册 shopify 英文网店 可以获得14天免费试用,注册完成后 您可以设置您的网站,添加您产品,快速预览你的网店 , 开启国际电子商务之旅注册时注意以下几点 1…
April 21, 2016
Magento : 调用文件上传 upload file frontend
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
$pathname
should be the server path to the file and not a url
Try
$path = Mage::getBaseDir('media') . DS;
You should try using Varien_File_Uploader
include_once Mage::getBaseDir() . '/lib/Varien/File/Uploader.php'; /* Starting upload */ $uploader = new Varien_File_Uploader('filename'); // Any extention would work $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(false); // Set the file upload mode // false -> get the file directly in the specified folder // true -> get the file in the product like folders // (file.jpg will go in something like /media/f/i/file.jpg) $uploader->setFilesDispersion(false); // We set media as the upload dir $path = Mage::getBaseDir('media') . DS; $uploader->save($path, $_FILES['filename']['name']);
参考:http://magento.stackexchange.com/a/40744
如果想要裁图的话,可以参考下面代码:
include_once Mage::getBaseDir() . '/lib/Varien/File/Uploader.php'; $file = new Varien_Io_File(); $imageDir = Mage::getBaseDir('media') . DS . 'mbimages'; $thumbimageyDir = Mage::getBaseDir('media').DS.'mbimages'.DS.'thumbs'; if(!is_dir($imageDir)) $imageDirResult = $file->mkdir($imageDir, 0777); if(!is_dir($thumbimageyDir)) $thumbimageDirResult = $file->mkdir($thumbimageyDir, 0777); //echo '<pre>'; print_r($_FILES); echo '==>'.$filename; echo '</pre>'; die; if(isset($_FILES[$filename]['name']) && $_FILES[$filename]['name'] != '') { //echo '==>'.$_FILES[$filename]['name']; try { /* Starting upload */ $uploader = new Varien_File_Uploader($filename); // Any extention would work $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); $uploader->setAllowRenameFiles(true); // Set the file upload mode // false -> get the file directly in the specified folder // true -> get the file in the product like folders // (file.jpg will go in something like /media/f/i/file.jpg) $uploader->setFilesDispersion(true); // We set media as the upload dir //$path = Mage::getBaseDir('media') . DS ; $path = $imageDir . DS ; $result = $uploader->save($path, $_FILES[$filename]['name']); $file = str_replace(DS, '/', $result['file']); ############################################################################### // actual path of image $imageUrl = Mage::getBaseDir('media').DS."mbimages".$file; // path of the resized image to be saved // here, the resized image is saved in media/resized folder $imageResized = Mage::getBaseDir('media').DS."mbimages".DS."thumbs".DS."mbimages".$file; // resize image only if the image file exists and the resized image file doesn't exist // the image is resized proportionally with the width/height 135px if (!file_exists($imageResized)&&file_exists($imageUrl)) : $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(FALSE); $imageObj->keepFrame(FALSE); $imageObj->quality(100); $imageObj->resize(80, 50); $imageObj->save($imageResized); endif; $data[$filename] = 'mbimages'.$file; } catch (Exception $e) { $data[$filename] = 'mbimages'.'/'.$_FILES[$filename]['name']; } }
更多图片处理方法请参考:magento图片处理 Customize Magento’s Image Resize Functionality
本文:Magento : 调用文件上传 upload file frontend