Magento Forum

   
Page 2 of 3
Add Media Uploader to Custom Module
 
raven
Jr. Member
 
Total Posts:  10
Joined:  2008-07-08
Philippines
 

Thanks somesid!

I tried the updated Module Creator and the Upload Field was working great.  cheese

 
Magento Community Magento Community
Magento Community
Magento Community
 
Dumpy Dooby
Jr. Member
 
Total Posts:  14
Joined:  2008-05-08
 

I’m really confused about what just happened. I’m not sure that I get how to implement that code into Magento without messing things up. Can someone explain how to add an upload field to the product view page?

 
Magento Community Magento Community
Magento Community
Magento Community
 
vibhor
Jr. Member
 
Total Posts:  15
Joined:  2008-08-29
 

Hi,

I can upload the file successfully now.(I used the module created v 0.9). However, when i edit the item, i see very samll thumnail image of the uploaded file along with the upload box and a delete option. I am not sure what I need to tweak in the code files.

Also, keeping these as it is, when I save, I get an error message:

Warningpreg_match() expects parameter 2 to be string, array given  in /home/piprojects/Testings/magento/lib/Varien/Data/Form/Element/Image.php on line 56

Can anyone please help me to sort this out. I hope this will definitely help others as well.

Thanks,
Vibhor

 
Magento Community Magento Community
Magento Community
Magento Community
 
tonythemediaguy
Member
 
Total Posts:  31
Joined:  2008-08-16
 

Hi,

Got the custom module with the uploader installed. How can I get a list of my products so that when upload a press release pdf I can associate that file and news entry with the product. So that when you’re looking at the news section on the front, it automatically can be linked over to a product. Or if I want to upload multiple files/news entry per product.

Or would a better/different option be to hack the media uploader into the product module so that when I’m adding a new product I can add a referenced file there. Like if I wanted them to listen to a preview of a song on the CD. I know downloadable products is coming in 1.2 and will probably give me this feature, even though my products are not downloadable. But I need to get these previews working in the current version of Magento.

Any pointers in the right direction would be great. The search on these forums is awful IMHO.

Tony

 
Magento Community Magento Community
Magento Community
Magento Community
 
bravehartk2
Member
 
Avatar
Total Posts:  34
Joined:  2008-08-14
 

Hallo Guys,
can some body tell me how to add a multiple file Upload with the media_uploader, so that the uploadet Files are shown in the form? I want to built an multiple Image upload like the product imgage upload in my own module. but when i use the adminhtml/media_uploader in a tab, i only get the Browse and upload buttons. But i need a Image preeview to. Is it possible to get the uploadet images shown by Ajax or so?

 
Magento Community Magento Community
Magento Community
Magento Community
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

Ok I had time to look on the bug people had using an image field type (the preg_match thing). Here is the 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']);
}

To answer the last post, to get an image preview right after uploading you have to do some custom code I guess. And it’s probably not that easy to make it work with magento.

 
Magento Community Magento Community
Magento Community
Magento Community
 
shua
Jr. Member
 
Total Posts:  13
Joined:  2009-05-12
 
vibhor - 09 October 2008 10:03 PM

Hi,

I can upload the file successfully now.(I used the module created v 0.9). However, when i edit the item, i see very samll thumnail image of the uploaded file along with the upload box and a delete option. I am not sure what I need to tweak in the code files.

Also, keeping these as it is, when I save, I get an error message:

Warningpreg_match() expects parameter 2 to be string, array given  in /home/piprojects/Testings/magento/lib/Varien/Data/Form/Element/Image.php on line 56


Can anyone please help me to sort this out. I hope this will definitely help others as well.

Thanks,
Vibhor

I was having the same problem, and although Devpen was close is solution didn’t quite work ( the image handling functions no longer worked, such as the delete image checkbox and the preview thumbnail )

What I did to make it work was in the file:

local/Company/Modulename/controllers/Adminhtml/ModulenameController.php

look for a similar block of code

if(isset($data['imagename']['delete']) && $data['imagename']['delete'== 1){
    $path 
Mage::getBaseDir('media');
    if(
file_exists($path."/".$data['imagename']['value'])) unlink($path."/".$data['imagename']['value']);
    
$data['imagename''';
}

and add an ‘else’ condition to it that will set the value to the hidden field produced by the ‘image’ output so it looks like this

if(isset($data['imagename']['delete']) && $data['imagename']['delete'== 1){
    $path 
Mage::getBaseDir('media');
    if(
file_exists($path."/".$data['imagename']['value'])) unlink($path."/".$data['imagename']['value']);
    
$data['imagename''';
  
}else{
    $data[
'imagename'$_POST['imagename']['value'];
  
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
Herve Guetin
Sr. Member
 
Avatar
Total Posts:  216
Joined:  2008-05-10
Lyon, FR
 

Hi’ll

And thanks somersid for this great module I’ve been using for several months now. Until now, I didn’t use it to upload images. I just tested the module today and faced the “preg_match thing” which got fixed with the code you gave a few posts above.

But when deleting the image from an item in the backend, the image itself is not deleted on the server…

Is there a way to do that and thus avoid increasing the load for diskspace on the server ?

Many thanks !

 
Magento Community Magento Community
Magento Community
Magento Community
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

It’s weird ‘cause the unlink($path."/".$data[’imagename’]) should delete the picture from the server, maybe it’s a problem of rights. I will look into it one of those days if I have some time.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jazkat
Sr. Member
 
Total Posts:  154
Joined:  2009-07-09
 

Thanks everyone for this code!

But I had to change the code a bit, because I had the same issues as devpen and then shua when saving - it said something like Can’t convert string to array in my case.
And though their solutions helped they didn’t solved my problems.

So now my saveAction() function now contains :

#-------- Upload file ----------#        

            if (isset($_FILES['image_url']['name']) ) {
            
              
if (isset($_POST['image_url']['value'])) 
                  
$data['image_url'$_POST['image_url']['value'];
            
              
// checks if delete checkbox is selected. If so, it deletes OLD file.
              
if ((isset($_POST['image_url']['delete'])) && ($_POST['image_url']['delete'== 1)){
                  $pathToFile 
Mage::getBaseDir('media') . DS $_POST['image_url']['value';
                  if (
file_exists($pathToFile)) // if file exists
                      
unlink($pathToFile)// delete file
                  
$data['image_url''';
              
}
              
try {
                  $uploader 
= new Varien_File_Uploader('image_url');
                  
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
                  
$uploader->setAllowRenameFiles(true);
                  
// setAllowRenameFiles(true) -> move your file in a folder the magento way
                  // setAllowRenameFiles(true) -> move your file directly in the $path folder
                  
$uploader->setFilesDispersion(false);
                  
$path Mage::getBaseDir('media') . DS ;
                  
                  
$uploader->save($path$_FILES['image_url']['name']);
                  
$data['image_url'$_FILES['image_url']['name'];
              
}
              
catch(Exception $e{}
            }
           
// in case checkbox is selected and no other file is suggested for upload
            
else if ((isset($_POST['image_url']['delete'])) && ($_POST['image_url']['delete'== 1)){
                $pathToFile 
Mage::getBaseDir('media') . DS $_POST['image_url']['value';
                if (
file_exists($pathToFile)) // if file exists
                    
unlink($pathToFile)// delete file
                
$data['image_url''';
            
}
            
#-------- end of Upload file ----------#

What I didn’t know is I also need this code when I create “post”. The saveAction() function is when editing existing post.
So to have upload functionality when creating “post” I have postAction() function.
After this:

if ($data $this->getRequest()->getPost()) {
add:
#-------- Upload file ----------#        
            if (isset($_FILES['image_url']['name']) ) {
              
try {
                  $uploader 
= new Varien_File_Uploader('image_url');
                  
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
                  
$uploader->setAllowRenameFiles(true);
                  
// setAllowRenameFiles(true) -> move your file in a folder the magento way
                  // setAllowRenameFiles(true) -> move your file directly in the $path folder
                  
$uploader->setFilesDispersion(false);
                  
$path Mage::getBaseDir('media') . DS ;
                  
                  
$uploader->save($path$_FILES['image_url']['name']);
                  
$data['image_url'$_FILES['image_url']['name'];
              
}
              
catch(Exception $e{}
            }
            
#-------- end of Upload file ----------#

And of course after deleting the “post” you also want probably to delete file. So in deleteAction() function after:

public function deleteAction()
    
{
        $galerijaId 
$this->getRequest()->getParam('id'false);
 
        try 
{
            
            Mage
::getModel('galerija/galerija')->setId($galerijaId)->delete();
Add this part:
#-------- Delete file ----------#        
            $galerijaDBTableCollectionMage::getModel('galerija/galerija')->getCollection();
            
$fileDetails $galerijaDBTableCollection->getItemById($galerijaId);
            
$pathToFile Mage::getBaseDir('media') . DS $fileDetails['image_url';
            
            if (
file_exists($pathToFile))// if file exists
                
chmod ($pathToFile0755);
                
unlink($pathToFile)// delete file
            
}
            
#-------- end of Delete file ----------#

In my case everything works fine, except in this last function I get error: Warning: unlink. Permission denied. Interesting is I don’t get the same error in saveAction() function when I’m deleting file on two places. As I’m working on Win platform I can’t chmode file, though I have it in the code, so I hope it’ll work for you on Linux.

Also make sure your path to the file is the same in all functions!

Hope this is helpful.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sandone
Jr. Member
 
Total Posts:  1
Joined:  2009-12-04
 

Feel really dumb right now, mainly got stuck because of the enctype we forgot.

Regards

Sandone
Surendettement

 
Magento Community Magento Community
Magento Community
Magento Community
 
nelamurugan
Jr. Member
 
Avatar
Total Posts:  22
Joined:  2009-01-15
Chennai
 

@SwiftCol , thanks man, your technique saved me.

I just did this for image reload on edit page in the admin banner module.

$object = Mage::getModel(’banner/banner’)->load( $this->getRequest()->getParam(’id’));
$note = false;
if( $object->getFilename() ){
$note = ‘<br/> <a href=“‘.Mage::getBaseUrl(’media’)."banners/".$object->getFilename().‘“ target="_blank">
<img src=“‘.Mage::getBaseUrl(’media’)."banners/".$object->getFilename().‘“ style="width:240px;height:120px;"/><br/>View it in New window</a>’;
}else{
$note = ‘Banner Image for the showcase (999px × 430px)’;
}

$fieldset->addField(’filename’, ‘file’, array(
‘label’ => Mage::helper(’banner’)->__(’File’),
‘required’ => false,
‘note’ => $note,
‘name’ => ‘filename’,
));

Hope it will help someone

 
Magento Community Magento Community
Magento Community
Magento Community
 
postadelmaga
Jr. Member
 
Total Posts:  6
Joined:  2011-03-27
 

Hello everybody,

I ‘m developing a new module that should upload some images and I would like to use the magento functions.

I have some problem understanding how to upload multiple images with varien file uploader.

I would like to have some interface like the edit page for product has .

I mean:

- How to add the functionality to edit uploaded images (image grid like in product section ) ?
( actually when I upload images using Varien File uploader, it generates a new row, without preview, of the new images )

- How to add previous uploaded images to Varien file uploader so that I can see them in the image grid

Image Attachments
screen.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
 
Mondane
Jr. Member
 
Total Posts:  26
Joined:  2011-09-19
 

With help from the comments in this forum, I managed to really implement the media gallery as seen in the product backend. Here’s my solution (feel free to improve on it, but remember to mention the changes here for everyone else to see/discuss):

Assumptions:

- Your custom module backend can be reached under <domain>/index.php/admin/custommodule_action or <domain>/admin/custommodule_action, ie. be a part of Magento’s Adminhtml module.
- You know the basics for setting up a module with models/resources.
- You know how layout.xml files work.
- Code can contain errors because I had to remove non-relevant parts.
- For the strings that are to be translated, the catalog helper is kept, no need to re-enter strings in your mymodule translate csv.

Step 1: Create your media table and add type fields to your model, code is added for your install / upgrade file
Step 2: add the media tab to your module backend
Step 3: add the class for block ‘mymodule_mycontroller_myaction_tab_media’, most likely in ‘app/code/local/mycompany/mymodule/Block/Adminhtml/mycontroller/myaction/Tab/Media.php
Step 4: add the gallery helper mentioned in the code for step 3, most likely in ‘app/code/local/mycompany/mymodule/Block/Adminhtml/mycontroller/Helper/Form/Gallery.php’
Step 5: add content block called in the gallery helper, most likely in ‘app/code/local/mycompany/mymodule/Block/Adminhtml/mycontroller/Helper/Form/Gallery/Content.php’
Step 6: add the gallery helper template, most likely in ‘app/design/adminhtml/default/mycompany/template/mymodule/helper/gallery.phtml’
Step 7: add the media config, most likely in ‘app/code/local/mycompany/mymodule/Model/mymodel/Media/Config.php’
Step 8: add the controller for the flash uploader, most likely in ‘app/code/local/mycompany/mymodule/controllers/Adminhtml/mymodule/mycontroller/GalleryController.php’
Step 9: add the image model mentioned in step 8’s code, most likely in ‘app/code/local/mycompany/mymodule/Model/mymodel/Image.php’
Step 10: add the functions to link the uploaded tmp files to your model, this goes in your mycontrollerController.php
Step 11: your mymodel needs some changes too
Step 12: add the media model, most likely in ‘app/code/local/mycompany/mymodule/Model/mymodel/Media.php’
Step 13: add the media resource and collection, these are just as default, I used:
Step 14: and finally, add the image helper, most likely in ‘app/code/local/mycompany/mymodule/Helper/Image.php’

Since one can only post 6000 chars, look in the attachments for the complete post.

File Attachments
Magento-add-media-gallery_bbcode.txt  (File Size: 46KB - Downloads: 629)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Bnito
Jr. Member
 
Total Posts:  9
Joined:  2009-12-16
 

@Mondane

I have a problem with uploading images, it returns me an error for each image.

Image Attachments
Image 8.png
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 2 of 3