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()) {

Add:

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

// Any extention would work
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);

// 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)
$uploader->setFilesDispersion(false);

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