Add an image field in your admin plugin (by example AW Blog)

Schermafbeelding 2013-07-03 om 14.28.17

Add an image field to plugins like AW Blog

Edit in /app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

$fieldset->addField('filename', 'file', array(
'label' => Mage::helper('blog')->__('Image'),
'required' => false,
'name' => 'filename',
'after_element_html' => 'Formaat 125 x 118 pixels',

Edit in /app/code/community/AW/Blog/controllers/Manage/BlogController.php after:

public function saveAction() {

if ($data = $this->getRequest()->getPost()) {


if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
try {
/* Starting upload */
$uploader = new Varien_File_Uploader('filename');

// Any extention would work

// 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)

// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS ;
$uploader->save($path, $_FILES['filename']['name'] );

} catch (Exception $e) {


//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];

Further add an field ‘filename’ in your db (table ‘aw_blog’).

And finally in /app/code/community/AW/Blog/Block/Blog/Edit/Form.php make sure that $form variable is defined as follows, so it works with multipart/form-data:

$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',

16 thoughts on “Add an image field in your admin plugin (by example AW Blog)”

  1. hi!

    just found your post explaining just what i need! The only thing i dont seem to get working is the part in:

    I dont really know if the code should be in the function _prepareForm or somewhere else! Could you please let me know!


    1. Thanks for your repsonse. Indeed it contained some bugs. I updated the post. It should work correctly. Can you confirm it if it works?

  2. Any idea what im doing wrong?

    I added all the code but its not saving the info in the database.
    I do see the image in the media map.

    I added filename to the database with varchar(255) utf8_general_ci

    1. Hi, I face the same issue.
      The image doesn’t get saved to the table.
      Did you got this issue solved? 🙁

  3. Hi,
    same thing here. The new field is displayed in backend but not stored in the database and not displayed when saved.

    What is missing (i use the last version of aw_blog).


  4. Its not working for me. I created a field named ‘filename’ in database after adding above code, but nothing worked for me.

  5. For everyone who has problems saving the file to the database, please refresh your indexes.

  6. Tks for this, it’s works great! I do encounter a small issue – when the post is edited, the image is deleted from DB. Any clue? Tks for help

  7. You just need to add below line
    in app/code/community/AW/Blog/controllers/Manage/BlogController.php file.

  8. Is there a way of implementing this within an extension rather than amending AW Blogs core code? So that AW Blog could be updated in the future and you could retain any additional fields added?

  9. I have an issue when you upload an image and click save, it saves with the image but if you go back edit the post content then click save it wipes out the image, and you have to re add it again :/

  10. This works great for me, thanks v much for the guide!

    One quick amendment. The version I’m using has a slightly different file structure.

    Instead of “/app/code/community/AW/Blog/controllers/Manage/BlogController.php”



    (Just the addition of the adminhtml folder)

Leave a Reply

Your email address will not be published. Required fields are marked *