How to Access Uploaded Files Using Magento Framework

0

Hello Friends,

In this blog, we will check How to access uploaded files using Magento Framework.

As you know in PHP when we use the input type file then we use the $_FILES variable to access that uploaded files but as per Magento standard we didn’t need to use the $_FILES variable because Magento provides the way which helps the developer to get that uploaded files.

Let’s see the example How you can get the uploaded files in Magento 2.

<?php
use Magento\Framework\App\RequestInterface;

class Test
{
   public function save() {
      $files = $this->getRequest()->getFiles(); // same as $_FILES
      echo '<pre>';print_r($files);die('die');
      // DO YOUR LOGIC HERE...
}

In the above code, there are no methods for getting files. But in implementation of the RequestInterface, there is a method that returns requested files data array.

Quick Tip:- How to Pass the System Configuration Value to Knockout?

Let’s see some PHP file upload errors which usually occur when you upload any file and submit it.

0 => 'There is no error, the file uploaded with success',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',

If you get any error code from 1 to 8 number then please check the above specific error towards error number.

Thanks for reading this post!

About the author

I’m Magento Certified Developer having quite 5 years of commercial development expertise in Magento as well as in Shopify. I’ve worked primarily with the Magento and Shopify e-commerce platform, managing the complexities concerned in building e-commerce solutions tailored to a client’s specific desires.

Related Posts

Leave a Reply