2

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

12

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

2
  • I have a similar issue on 2.3.3, but unfortunately, edit value upload_tmp_dir did not help. $_FILES["file"]["tmp_name"] and sys_get_temp_dir() are different. Server Nexcess. Reson: "This actually makes sense, because /tmp is a symbolic link to /chroot/home/tmp on our servers." – Oleksiy Nov 29 '19 at 8:55
  • There's also an open issue: github.com/magento/magento2/issues/25835 – Rob Olmos Mar 5 '20 at 18:46
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). – Rico Neitzel May 2 '20 at 11:17
  • It's not a valid solution. – Khoa TruongDinh May 31 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

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, privacy policy and cookie policy

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