1

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.

3

Add this before calling $uploader->save

$uploader->setAllowCreateFolders(true);

This will create the folder if it does not exist.
Then you can change your destination folder like this:

$media_path  = Mage::getBaseDir('media') . DS. 'newfolder' . DS;
  • it is showing the same error this time also. the error is """Fatal error: Call to undefined method Vertax_Blog_Adminhtml_ArticleController::redirectError() in /opt/lampp/htdocs/magento_new/app/code/local/Vertax/Blog/controllers/Adminhtml/ArticleController.php on line 112""" and the line is the catch section of the save action – Waseem Feb 14 '14 at 9:48
  • @waseem. Maybe you should post your full controller. I don't think this has something to do with the image upload. I think you are calling a method redirectError that should be _redirectError. redirectError does not exist. – Marius Feb 14 '14 at 9:51
  • Thanks for that, now the error is gone but still the image is not saved in the newfolder. I am posting my controller in the question. – Waseem Feb 14 '14 at 9:55
  • have you seen anything what is happening wrong over here – Waseem Feb 14 '14 at 10:04
  • @waseem. It looks ok to me at a first glance. I see that you log the exception when uploading the file. Check var/log for more details. It should give you an idea of what's happening. – Marius Feb 14 '14 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.