1

How can I use $_FILES (Super global variable )in Magento2 system configuration as per ECG coding standard. I know I can use in simple form like below. where my_file_uploader is name of fieldId

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

But whats is the fieldId in system configuration ?

I have tried several way but couldn't succeed. I have tried

$uploader = $this->_objectManager->create(
      'Magento\MediaStorage\Model\File\Uploader',
      ['fileId' => '[groups][MY_GROUP][fields][import][value]'
    );

but it gives me error like

Undefined index: in /var/www/html/magento21/vendor/magento/framework/File/Uploader.php on line 542

any help will be appreciated

Thanks.

  • 1
    Hi keyur !! try with UploaderFactory .. see this file for reference - \vendor\magento\module-catalog\Model\ResourceModel\Product\Attribute\Backend\Image.php line 41 and 62 .. As for fileId it seems to be the key of $_FILES array. Because tracing down fileId it uses it as follows $_FILES[$fileId]; .. See here vendor\magento\framework\File\Uploader.php line 552 .. Not sure if this addresses your question but hope it helps :) – Reena Parekh Sep 10 '16 at 11:34
  • 1
    @ReenaParekh thanks for the code hunt. I am already using UploaderFactory , and it's working fine for me in my custom module. But when I am using this in system configuration it creates problem for me as you know we can't define it's name :) – Keyur Shah Sep 10 '16 at 11:46
  • 1
    and format of fileId will probably be something like ['groups'][your_group_id]['fields'][your_field_Id]['value'] – Reena Parekh Sep 10 '16 at 11:47
  • 1
    Thanks @ReenaParekh for your time ! I got the solution please check below answer :) – Keyur Shah Sep 13 '16 at 13:42
4

I have follow below steps :

1) Add system configuration

<field id="import" translate="label" type="Namespace\Module\Block\Adminhtml\Form\Field\Import" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Import</label>
    <backend_model>Namespace\Module\Model\Config\Backend\Import</backend_model>
</field>

2) Create Import.php block file and crete import element

<?php

namespace Namespace\Module\Block\Adminhtml\Form\Field;

class Import extends \Magento\Framework\Data\Form\Element\AbstractElement
{

    protected function _construct()
    {
        parent::_construct();
        $this->setType('file');
    }

    public function getElementHtml()
    {
        $html = '';

        $html .= '<input id="time_condition" type="hidden" name="'.$this->getName().'" value="'.time().'" />';

        $html .= '<input id="custom_sample_import" name="customimport" data-ui-id="file-groups-sample-fields-import-value" value="" class="" type="file" />';

        return $html;
    }
}

3) Use below code for uploader

$uploader = $this->_objectManager->create('Magento\MediaStorage\Model\File\Uploader', ['fileId' => 'customimport']);
  • hello, can please update your answer, where can i write $uploader = $this->_objectManager->create('Magento\MediaStorage\Model\File\Uploader', ['fileId' => 'customimport'] with full class? – chirag Nov 25 '16 at 5:27
  • i cant understand your issue, but you have to add this code where you want to add. Or are you want to declare uploader class in __construct function ? – Jaimin Parikh Nov 25 '16 at 6:41
  • i am trying to upload CSV file in my custom table from system config. when i use this it's give me message success but its nothing do. not my echo die in model. – chirag Nov 25 '16 at 6:46
  • i want to save csv file data in my table after save system config. thats why i have use afterSave() method in backend model. in your case Namespace\Module\Model\Config\Backend\Import. but it not going in that method. – chirag Nov 25 '16 at 7:59
  • Hello @JaiminParikh, We are having the same issue like this and when we tried your code it worked, and change the name of file field. But when click the save it won't come in saveAfter method (which works with default) of my back-end Model class, where I want to get the import file data. Can you please suggest, in which method we can get the Data of my imported CSV file. – Ashish Jagnani Nov 25 '16 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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