3

I used $_FILES when uploading files, but magento-connect do not allow to use global variables, and it generates an error in the validation report:

Direct use of $_FILES Superglobal detected

How can I fix this error?

1
  • I am using import feature in system configuration, then what will be the fileId value ? @Asif
    – Keyur Shah
    Sep 10, 2016 at 5:09

1 Answer 1

4

We had the same issue but a little search in the core and also this link helped us figure out the solution

you've to use

$uploader = $this->_objectManager->create(
  'Magento\MediaStorage\Model\File\Uploader',
  ['fileId' => 'my_file_uploader']
);

in place of $_FILES and it's much better like this, you can access various methods in the object. To check the output, use:

 var_dump($uploader->validateFile());

this will return an array with details about the file uploaded.

Hope it helps.

5
  • 1
    using OM directly like this is not a good idea. this can be done much simpler via the ui components, but maybe they weren't available when this answer was submitted Jul 16, 2016 at 21:09
  • I am using import feature in system configuration, then what will be the fileId value ? @ClaudiuCreanga @R T
    – Keyur Shah
    Sep 10, 2016 at 5:08
  • 1
    @KeyurShah if it is to upload a file in system config you can check this github.com/ClaudiuCreanga/… if not maybe ask a question Sep 10, 2016 at 11:48
  • Thanks a ton @ClaudiuCreanga It really helps me a lot to resolve the issue. :)
    – Keyur Shah
    Sep 10, 2016 at 15:50
  • when we decide to not upload the image as its not mandatory it gives an exception, how can we validate before using above ? Dec 15, 2017 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.