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?
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:
Warning: preg_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.
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.
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?
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.
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.
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:
Warning: preg_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 )
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 ?
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.
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);
if (file_exists($pathToFile)){ // if file exists chmod ($pathToFile, 0755); 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!
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
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.