1

I have a form in my custom module with all the fields saved in the database table, now I have a created an image upload form in the next section of the tab which saves the data in the different table from the above mention table.

I have successfully uploaded the image from the backend, and now I want to show those images in frontend.

I have searched the internet for quite a long time but I didn't get how can I show those images.

I showed all the content of the first table on frontend but I have no idea how to do that for the images. Please someone help me with this.

EDIT

Here is my controller save function

public function saveAction()
   {
     if ($this->getRequest()->getPost())
     {
       try {
             $postData = $this->getRequest()->getPost(); 
     $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();

                Mage::getSingleton('adminhtml/session')
                           ->addSuccess('successfully saved');
                Mage::getSingleton('adminhtml/session')
                            ->setarticleData(false);
                $this->_redirect('*/*/');
            if ($this->getRequest()->getParam('back')) {
    $this->_redirect('*/*/edit',array('id' => $articleModel->getId()));
        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('*/*/');
        }
4

does the ./media/blog/ exist and is it writable? And what is $postData['image'] value. Is it filled in the form with the intended name for the image? I don't think it's used anywhere.

Also, since you're saving the image to a different database table than the blog you will need to load a second model on the frontend. And since it seems you can have multiple images it should be a collection you're loading

$article = Mage::getModel('blog/article')->load([the required id]);
$image_collection = Mage::getModel('blog/image')->getCollection()
  ->addFilterToFilter('article_id', $article->getId());

foreach ($image_collection as $image):
   echo '<img src="' . Mage::getBaseUrl('media').'/blog/'.$image->getData('image') . '"/>';
endforeach;

if the images are not displayed debug the following way

  1. Put a try{}catch{} around the save methods in the controller to see if an error is given while saving
  2. Make sure there is actually data saved in the database and the data is correct through PHPMyAdmin or MySQL CLI
  3. check the size of the collection if there are actually items begin loaded
  4. print the $image->getData() to see what data it contains
  • yes .media/blog exists and it is writable. and $postData['image'] has an image name – Waseem Feb 20 '14 at 14:48
  • but what is the image name used for, you're added it to the $imgFilename variable but then it's never used anywhere else – Sander Mangel Feb 20 '14 at 14:49
3

I assume you have a model that maps on your images tables.
I also assume you place all the images in a specific folder.
If my assumptions are wrong then you should revise your code :).

Let's say the image field name is called 'thumb' and the folder where you store the images is media/custom.

To show an image, using the model do this:

<?php $someId = 5;
$model->load($someId);
?>

<img src="<?php echo Mage::getBaseUrl('media').'/custom/'.$model->getThumb()?>" alt="" />

Some duplicate slashed may appear depending on how you save the values in your db, but that can be easily fixed.

  • thanks for quick reply, but i am unable to show the image. my image are in the media dir, not in any folder. and my model is not returning anything. – Waseem Feb 20 '14 at 14:19
  • so the path to the image file is stored in the database along with the rest of the record? – Sander Mangel Feb 20 '14 at 14:31
  • @Waseem. Add some of your code to the question. At least the way you save your images. – Marius Feb 20 '14 at 14:32
  • question is updated – Waseem Feb 20 '14 at 14:38
  • @SanderMangel Iam not storing the path in the database just the image name and the id of the article associated with it. – Waseem Feb 20 '14 at 14:45

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.