3

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

7 Answers 7

14

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

4
1

Go to vendor/magento/framework/File/Uploader.php

In function _setUploadFileId($fileId), change:

$this->validateFileId($fileId);

to

//$this->validateFileId($fileId);

this worked for me

2
  • 1
    by simply removing the validation, you don't solve the problem. You just ignore it. And: It's never a good idea to change a file in vendor/* as it will be rewritten on every deployment (composer install/update). Commented May 2, 2020 at 11:17
  • It's not a valid solution. Commented May 31, 2021 at 4:48
0

Valid solution:

  • update php.ini variable "sys_temp_dir"

I was getting issue on bitnami AWS server.

Edit: /opt/bitnami/php/etc/php.ini

Set: sys_temp_dir = "/opt/bitnami/php/tmp"

Restart apache server.

Thanks,

0

Solution for Windows


Run the following command. You should get a path (I am using <YOUR_PATH_OUTPUT> as a placeholder). Copy that path.

$ php -a
Interactive shell

php > echo sys_get_temp_dir() . PHP_EOL;
<YOUR_PATH_OUTPUT>

Now open your php.ini file and search for sys_temp_dir and paste the path here.

sys_temp_dir = '<YOUR_PATH_OUTPUT>'

Do not forget to restart XAMPP after this.

0

@user84389

Hey,

Go to vendor\magento\framework\File\Uploader.php

In function _setUploadFileId($fileId), change:

$this->validateFileId($fileId);

to

//$this->validateFileId($fileId);

That’s all for resolving the error.

Thank You!

-1

For windows do 2 things

  1. open php.ini file and set sys_temp_dir = "Your valid writable path"
  2. Overwrite file Magento\Framework\File\Uploader and change in function validateFileId.

change if (stripos($tmpName, $allowedFolder) === 0) {

to

if (stripos($tmpName, $allowedFolder) === false) {

-2

Go to vendor\magneto\framework\File\Uploader.php

In function _setUploadFileId($fileId), change:

$this->validateFileId($fileId); To //$this->validateFileId($fileId);

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.