Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 3 of 3
Add Media Uploader to Custom Module
 
Mondane
Jr. Member
 
Total Posts:  26
Joined:  2011-09-19
 

If you are running Magento 1.6.1, you are probably experiencing this:

http://www.magentocommerce.com/boards/viewthread/263751/#t357355

Or, if you are running Magento on a HTTPS URL with a self-signed certificate (ie, you have to add an exception to your browser before it lets you see the browser), this won’t work with the flash uploader. The uploader only works with HTTP or with a valid HTTPS certificate.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lukefowell89
Jr. Member
 
Total Posts:  3
Joined:  2011-10-11
 

Hi there,

I have been struggling with this issue for the last couple of days. I need as a matter of urgency to create a module to handle image uploads and management, i.e. a media gallery.

At the moment I have a section in the action which allows me to save the the details for a page in, but I need to be able to upload and edit images too.

Mondane, you posted some interesting information, however I do not follow it completely and it does not quite work with my current system.
I have a form which creates a row of an image uploading field, a couple of text fields for title, etc and sort order. I need to be able to store the image, and then save the url in the db. Any pointers? At the moment I don’t know how to retrieve files through magento, but I know that $_FILES is returning a blank array, even though I have multipart/form-data set correctly....

Can someone give me some pointers of how to access the uploaded image files?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mondane
Jr. Member
 
Total Posts:  26
Joined:  2011-09-19
 

From this post: http://www.magentocommerce.com/boards/spip.php/viewreply/358889/ can you tell me at which step you are stuck?

And, if possible, can you attach the module you’re working on.

PS Which Magento version do you have?

 
Magento Community Magento Community
Magento Community
Magento Community
 
AndreasRLC
Jr. Member
 
Total Posts:  4
Joined:  2011-10-11
 

@Mondane

I\’m not sure I follow your guide when you write in step 1, add to the install / upgrade file.

Could you by any chance post a demo, where the code is distributed between their respective files?

Best regards
Andreas

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mondane
Jr. Member
 
Total Posts:  26
Joined:  2011-09-19
 
AndreasRLC - 27 January 2012 04:01 AM

@Mondane

I\’m not sure I follow your guide when you write in step 1, add to the install / upgrade file.

Could you by any chance post a demo, where the code is distributed between their respective files?

Best regards
Andreas

The code in the attachment for step 1, lines 14-112, goes in ie /app/code/local/MyCompany/MyModule/setup/mymodule/install.php or /app/code/local/MyCompany/MyModule/setup/mymodule/upgrade-x-y.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
AndreasRLC
Jr. Member
 
Total Posts:  4
Joined:  2011-10-11
 

Thank you very much, I was unsure about the install part of the modules.

Still, I’m unable to get mine to work, it keeps giving a server error (without logging).

Could you by any chance post an example module, where the code is structured as it should to work?

Best regards
Andreas

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mondane
Jr. Member
 
Total Posts:  26
Joined:  2011-09-19
 
AndreasRLC - 01 February 2012 06:46 AM

Thank you very much, I was unsure about the install part of the modules.

Still, I’m unable to get mine to work, it keeps giving a server error (without logging).

Could you by any chance post an example module, where the code is structured as it should to work?

Best regards
Andreas

I don’t have one right now. Can you post the server error, perhaps in /var/log/error.log ?

Edit [1 feb, 20:14 CET] Placed the code from my earlier attachment in the corresponding files. It’s not a working example, but perhaps it helps finding out your problem.

File Attachments
mediagallery.zip  (File Size: 24KB - Downloads: 222)
 
Magento Community Magento Community
Magento Community
Magento Community
 
AndreasRLC
Jr. Member
 
Total Posts:  4
Joined:  2011-10-11
 

Thank you very much, you\\\’re being very helpful!

The module I\\\’m writing does not use tabs, so I\\\’m unsure how I display it through my module controller.
I don\\\’t suppose I could display the gallery through block creation? I\\\’ve tried but it gives an error.

I guess my question is: How do I display the widget in my layout?

Best regards
Andreas

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mondane
Jr. Member
 
Total Posts:  26
Joined:  2011-09-19
 
AndreasRLC - 03 February 2012 04:21 AM

Thank you very much, you\\\’re being very helpful!

The module I\\\’m writing does not use tabs, so I\\\’m unsure how I display it through my module controller.
I don\\\’t suppose I could display the gallery through block creation? I\\\’ve tried but it gives an error.

I guess my question is: How do I display the widget in my layout?

Best regards
Andreas

I don’t believe you can use the media gallery without tabs. It shouldn’t be that hard to convert your module to a tabbed layout, this site can help:

http://www.excellencemagentoblog.com/admin-part1-series-magento-admin-forms-grids-controllers-tabs
http://www.excellencemagentoblog.com/magento-part3-series-advanced-layouts

Good luck!

 
Magento Community Magento Community
Magento Community
Magento Community
 
zaheerabbasbadi
Jr. Member
 
Total Posts:  28
Joined:  2012-05-16
 

Hello anyone success with media uploader?if yes then please guide how to impliment media uploader in custom module.I had uploaded image successfully but don\\\’t know how to store uploaded image in DB and after uploaded images no message for succss and can\\\’t hide uploaded image div like at product page. Please help..

Image Attachments
Screenshot.png
 
Magento Community Magento Community
Magento Community
Magento Community
 
kads
Jr. Member
 
Total Posts:  3
Joined:  2012-09-02
 

How can i add multiple images with the use of uploader class?

 
Magento Community Magento Community
Magento Community
Magento Community
 
zaheerabbasbadi
Jr. Member
 
Total Posts:  28
Joined:  2012-05-16
 

I think you should take a look in this module.
Module Link

 
Magento Community Magento Community
Magento Community
Magento Community
 
Vbak
Jr. Member
 
Avatar
Total Posts:  1
Joined:  2013-09-04
 

@Mondane

Is there a reason why i can’t download the attached files?
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Getting an error when you resave that the image has become an array.

A fix would be the following, to state that the filename data.

($data $this->getRequest()->getPost()) {
$data[
'filename'$data['filename']['value'];

            if(isset(
$_FILES['filename']['name']) && (file_exists($_FILES['filename']['tmp_name']))) {
 
Magento Community Magento Community
Magento Community
Magento Community
 
ali_waseemali
Jr. Member
 
Total Posts:  4
Joined:  2013-03-13
 
somesid - 07 August 2008 08:09 AM

I must say I feel really dumb right now, mainly got stuck because of the enctype we forgot ! But here is how you make your upload work !!!

First lets suppose you created a custom module with the module creator.

Then in /app/code/local/Company/ModName/Block/Adminhtml/ModName/Edit/Form.php

Add ‘enctype’ => ‘multipart/form-data’, yeah ! That should help to get something in $_FILES ....

$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'
   
)
 );

Then in /app/code/local/Company/ModName/Block/Adminhtml/ModName/Edit/Tab/Form.php

in _prepareForm
Add your image field (you can add a field of type file or imagefile for vidéos !

$fieldset->addField('fileinputname''image', array(
          
'label'     => Mage::helper('pictos')->__('File label'),
          
'required'  => false,
          
'name'      => 'fileinputname',
));

Then in /app/code/local/Company/ModName/controllers/Adminhtml/ModuleNameController.php again
after if ($data = $this->getRequest()->getPost()) { in saveAction()
if($_FILES['fileinputname']['name'!= ''{

    
try {    
         $uploader 
= new Varien_File_Uploader('fileinputname');
         
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
         
$uploader->setAllowRenameFiles(false);
         
$uploader->setFilesDispersion(false);
     
         
$path Mage::getBaseDir('media') . DS ;
                
         
$uploader->save($path$_FILES['fileinputname']['name']);
    
catch (Exception $e{
          
    }

    $data[
'fileinputname'$_FILES['fileinputname']['name'];

}

EDITED WITH FIX : if you are using an image field type, (image is set in addField), then you should add the following code after the if in the controller code. It will now handle a “delete” checkbox linked to the upload field.

else {        
    
if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'== 1)
         
$data['fileinputname''';
    else 
        unset(
$data['fileinputname']);
}

That’s it you’re done.

I made some progress as well with the uploader from the product page, will post tomorrow.

I used your code and it works great. but i want to change the folder path as it stores images in the media folder and i want to store images in the newfolder under media. So when I change the folder path it shows an error.
Fatal error: Call to undefined method Vertax_Blog_Adminhtml_ArticleController::redirectError()

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 3 of 3