I have a custom form for uploading the images and it is working right.
But it saves the images in the media
folder and I want to save the images under some other folder in the media
folder.
Here is my save action in the controller
<?php
class Vertax_Blog_Adminhtml_ArticleController extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
$this->loadLayout()->_setActiveMenu('blog/article')
->_addBreadcrumb('Article Manager','Article Manager');
return $this;
}
public function indexAction()
{
$this->_initAction();
$this->_title($this->__("Vertax_Article"));
$this->renderLayout();
}
public function editAction()
{
$articleId = $this->getRequest()->getParam('id');
$articleModel = Mage::getModel('blog/article')->load($articleId);
$categoryModel= Mage::getModel('blog/category')
->getCollection()
->addFieldToFilter('article_id', $articleId);
if ($articleModel->getId() || $articleId == 0)
{
Mage::register('article_data', $articleModel);
$this->loadLayout();
$this->_setActiveMenu('blog/article');
$this->_addBreadcrumb('article Manager', 'article Manager');
$this->_addBreadcrumb('Article Description', 'Article Description');
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->_addContent($this->getLayout()->createBlock('blog/adminhtml_article_edit'))
->_addLeft($this->getLayout()->createBlock('blog/adminhtml_article_edit_tabs'));
$this->renderLayout();
}
else
{
Mage::getSingleton('adminhtml/session')
->addError('Article does not exist');
$this->_redirect('*/*/');
}
}
public function newAction()
{
$this->_forward('edit');
}
public function saveAction()
{
if ($this->getRequest()->getPost())
{
try {
$postData = $this->getRequest()->getPost();
//echo "<pre>";print_r($postData);
$articleModel = Mage::getModel('blog/article');
$imgFilename = NULL;
if($_FILES['image']['name'] != '')
{
try {
$uploader = new Varien_File_Uploader('image');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$uploader->setAllowCreateFolders(true);
// Set media as the upload dir
//$media_path = Mage::getBaseDir('media') . DS;
$media_path = Mage::getBaseDir('media') . DS . 'blog' . DS;
$imgFilename = $media_path . $postData['image'];
// Upload the image
$uploader->save($media_path, $_FILES['image']['name']);
}
catch (Exception $e)
{
Mage::log($e);
$this->_redirectError(502);
}
$data['image'] = $_FILES['image']['name'];
}
else
{
if(isset($data['image']['delete']) && $data['image']['delete'] == 1)
$data['image'] = '';
else
unset($data['image']);
}
$imageModel=Mage::getModel('blog/image');
if( $this->getRequest()->getParam('id') <= 0 )
$articleModel->setCreatedTime(
Mage::getSingleton('core/date')
->gmtDate());
$articleModel
->addData($postData)
->setUpdatedTime(
Mage::getSingleton('core/date')
->gmtDate())
->setId($this->getRequest()->getParam('id'))
->save();
$lastid = $articleModel->getId();
$imageModel->setArticleId($lastid)->setImage($data['image'])->save();
foreach($postData['category_id'] as $cat_id)
{
$categoryModel = Mage::getModel('blog/category');
if( $this->getRequest()->getParam('id') <= 0 )
$categoryModel->setCategoryId($cat_id)->setArticleId($lastid)->save();
$categoryModel->setCategoryId($cat_id)->setArticleId($this->getRequest()->getParam('id'))->save();
}
Mage::getSingleton('adminhtml/session')
->addSuccess('successfully saved');
Mage::getSingleton('adminhtml/session')
->setarticleData(false);
$this->_redirect('*/*/');
return;
}
catch (Exception $e)
{
Mage::getSingleton('adminhtml/session')
->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')
->setarticleData($this->getRequest()
->getPost());
$this->_redirect('*/*/edit',
array('id' => $this->getRequest()
->getParam('id')));
return;
}
}
$this->_redirect('*/*/');
}
public function deleteAction()
{
if($this->getRequest()->getParam('id') > 0)
{ $articleId=$this->getRequest()->getParam('id');
try
{
$articleModel = Mage::getModel('blog/article');
$articleModel->setId($this->getRequest()
->getParam('id'))
->delete();
$categoryModel= Mage::getModel('blog/category')
->getCollection()
->addFieldToFilter('article_id', array('eq'=>$articleId));
foreach ($categoryModel as $cat) {
$cat->delete(); }
Mage::getSingleton('adminhtml/session')
->addSuccess('successfully deleted');
$this->_redirect('*/*/');
}
catch (Exception $e)
{
Mage::getSingleton('adminhtml/session')
->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
public function massDeleteAction()
{
$catIds = $this->getRequest()->getParam('id');
if (!is_array($catIds)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
} else {
try {
foreach ($catIds as $catId) {
$cat = Mage::getModel('blog/article')->load($catId);
$cat->delete();
$categoryModel= Mage::getModel('blog/category')
->getCollection()
->addFieldToFilter('article_id', array('eq'=>$catId));
foreach ($categoryModel as $cat) {
$cat->delete();
}
}
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('adminhtml')->__('Total of %d record(s) were successfully deleted',
count($catIds))
);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massActiveAction()
{
$catIds = $this->getRequest()->getParam('id');
if (!is_array($catIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
} else {
try {
foreach ($catIds as $catId) {
Mage::getSingleton('blog/article')
->load($catId)
->setStatus('Active')
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($catIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massInactiveAction()
{
$catIds = $this->getRequest()->getParam('id');
if (!is_array($catIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
} else {
try {
foreach ($catIds as $catId) {
Mage::getSingleton('blog/article')
->load($catId)
->setStatus('Inactive')
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($catIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
}
?>
I tried changing the path form media
to media/newfolder
but whenever I tried to do something like this it shows an error.
How can I change the path.