Upload images in Magento programmatically

In our custom Magento modules, sometimes we need to be able to upload, save some images to our server. As we know, in Magento we can upload images for product, store logo … So we also can create a form which can be used to upload images and save them to the server. Let’s find out how to upload images in Magento.

What we need:

  • a form with a file input.
  • a controller to get request and save the images.
  • Assuming that we save all images in “/media” directory.

 

1. Create a form with content:

It’s simple, right ? Now we will create a controller with an action to get request from this form and process.

 

2. Create upload action in your controller class like this:

 

You can get uploaded file name and path in $result which is an array. You also can look into “/media” directory and see if the image is saved. Hope this post could help you upload images in Magento.

In the next post, we will tell you how to create this feature work in Magento 2.

 

Leave a Reply

Your email address will not be published.