1

system.xml

<field id="upload_image_id" translate="label comment" type="image" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
   <label>Select Image</label>
   <backend_model>NameSpace\YourModule\Model\Config\Backend\Image</backend_model>
   <base_url type="media" scope_info="1">yourfolder</base_url>
</field>

Image.php

namespace NameSpace\YourModule\Model\Config\Backend;

class Image extends \Magento\Config\Model\Config\Backend\Image
{
/**
 * The tail part of directory path for uploading
 *
 */
const UPLOAD_DIR = 'yourfolder'; // Folder save image

/**
 * Return path to directory for upload file
 *
 * @return string
 * @throw \Magento\Framework\Exception\LocalizedException
 */
protected function _getUploadDir()
{
    return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
}

/**
 * Makes a decision about whether to add info about the scope.
 *
 * @return boolean
 */
protected function _addWhetherScopeInfo()
{
    return true;
}

/**
 * Getter for allowed extensions of uploaded files.
 *
 * @return string[]
 */
protected function _getAllowedExtensions()
{
    return ['jpg', 'jpeg', 'gif', 'png', 'svg'];
}

Still not allowing it I get Disallowed file type. Anything else missing ?

3
  • Have you fixed it? Jul 21, 2021 at 5:28
  • @DhadukMitesh Need to create virtualType in di.xml Jul 21, 2021 at 16:30
  • Already created but give Disallowed file type error. Jul 21, 2021 at 18:28

3 Answers 3

0

you can try with below code in system.xml file: system.xml

<field id="upload_image_id" translate="label comment" type="Magento\Config\Block\System\Config\Form\Field\Image" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
               <label>Select Image</label>
               <backend_model>NameSpace\YourModule\Model\Config\Backend\Image</backend_model>
               <base_url type="media" scope_info="1">yourfolder</base_url>
</field>
4
  • Already tried with type="Magento\Config\Block\System\Config\Form\Field\Image" not working! Jul 16, 2020 at 8:29
  • @JulianoVargas, please check this link mageplaza.com/devdocs/…. I hope it will be helpfull for you. Jul 16, 2020 at 8:34
  • Yeah done that already as well. Doesn't work still! looked every possible blog out there but possible are outdated as i am on 2.3.5 and is fairly new version. Jul 16, 2020 at 8:38
  • can you please try my ans and let me know if you face any error
    – Pramod
    Jul 16, 2020 at 8:43
0

add below code in your system.xml file

<field id="my_image_field" translate="label" sortOrder="1" type="Magento\Config\Block\System\Config\Form\Field\Image" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>My Image</label>
        <backend_model>Vendor\Package\Model\Config\Backend\Image</backend_model>
        <base_url type="media" scope_info="1">my_image_folder</base_url>
        <validate>required-entry</validate>
        <upload_dir config="system" scope_info="1">my_image_folder</upload_dir>
    </field>

now your image.php

<?php

namespace Vendor\Package\Model\Config\Backend;

class Image extends \Magento\Config\Model\Config\Backend\File
{
    protected function _getAllowedExtensions()
    {
        return ['jpg', 'jpeg', 'gif', 'png', 'svg'];
    }
}

Please note that the image will be saved in magento_root/pub/media/my_image_folder

3
  • Nope. not working ! Jul 16, 2020 at 10:34
  • can you please share what is the error you are getting?
    – Pramod
    Jul 16, 2020 at 10:51
  • Disallowed file type. already checked the file type in File/Uploader _validateFile and is indeed svg when I upload. Jul 16, 2020 at 11:05
0

What works for me is a combination of the answers from here and from Mageplaza blog.

<field id="my_image" type="Magento\Config\Block\System\Config\Form\Field\File" translate="true" sortOrder="30" showInDefault="1" showInStore="1" showInWebsite="1">
    <label>Image</label>
    <backend_model>Vendor\Module\Model\Config\Image</backend_model>
    <upload_dir config="system">my_custom_folder</upload_dir>
</field>

and in Image.php class:

<?php 
    
    namespace Vendor\Module\Model\Config;

    class Image extends \Magento\Config\Model\Config\Backend\File
    {
        protected function _getAllowedExtensions()
        {
            return ['jpg', 'jpeg', 'gif', 'png', 'svg'];
        }
    }

And i can properly upload a file and when saving, i get the proper validation if i select a file with an extension other than the ones listed above:

enter image description here

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.