Magento – Invalid parameter given. A valid $fileId[tmp_name] is expected

image-uploadmagento2

I am trying to use an image uploader but it is generating error

Invalid parameter given. A valid $fileId[tmp_name] is expected.

I debugged and find out that the problem is in image uploader class of magento core.
please check my code.
my code in system.xml

<field id="image_upload1" translate="label" type="image" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Image Upload</label>
      <backend_model>Magento\Config\Model\Config\Backend\Image</backend_model>
      <upload_dir config="system/filesystem/media" scope_info="1">categoryBanner</upload_dir>
      <base_url type="media" scope_info="1">categoryBanner</base_url>
      <comment><![CDATA[Allowed file types: jpg, jpeg, gif, png, svg]]></comment> 
</field>

my code of image.php

namespace Afg\Customsettings\Model\Config\Backend;

class Image extends \Magento\Config\Model\Config\Backend\Image
{
    /**
     * The tail part of directory path for uploading
     *
     */
    const UPLOAD_DIR = '/categoryBanner'; // 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'];
    }
}

enter image description here

Best Answer

Issue: This seems to be a conflict between your selection of the tmp folder and the folders Magento considers secure for being used as tmp.

This can be observed in the validateFieldId() function in the class Magento\Framework\File\Uploader

[0] => string(15) "C:\WINDOWS\TEMP" <for windows and probably var/tmp for unix based systems>
[1] => string(30) "<magento_root>/pub/media"
[2] => string(24) "<magento_root>/var"
[3] => string(28) "<magento_root>/var/tmp"
[4] => string(37) "<magento_root>/pub/media/upload"

Solution: Simply reconfigure your php.ini and set the value upload_tmp_dir to one of the allowed folders of Magento. Make sure you are updating the right version of php.ini

Reference: I got the same issue and spent hours to get to the root of this and got it fixed by this small change.

Request: upvote my response if it worked for you