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.


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');
                     // 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) 
        $data['image'] = $_FILES['image']['name'];
        if(isset($data['image']['delete']) && $data['image']['delete'] == 1)
         $data['image'] = '';

                if( $this->getRequest()->getParam('id') <= 0 )
          $lastid = $articleModel->getId();

                           ->addSuccess('successfully saved');
            if ($this->getRequest()->getParam('back')) {
    $this->_redirect('*/*/edit',array('id' => $articleModel->getId()));
    catch (Exception $e)
                        array('id' => $this->getRequest()

2 Answers 2


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') . '"/>';

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
    Commented Feb 20, 2014 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 Commented Feb 20, 2014 at 14:49

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;

<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
    Commented Feb 20, 2014 at 14:19
  • so the path to the image file is stored in the database along with the rest of the record? Commented Feb 20, 2014 at 14:31
  • @Waseem. Add some of your code to the question. At least the way you save your images.
    – Marius
    Commented Feb 20, 2014 at 14:32
  • question is updated
    – Waseem
    Commented Feb 20, 2014 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
    Commented Feb 20, 2014 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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