cancel
Showing results for 
Search instead for 
Did you mean: 

getting error Warning: Invalid argument supplied for foreach()

getting error Warning: Invalid argument supplied for foreach()

while I run bin/magento setup:upgrade its throwing below error

"Warning: Invalid argument supplied for foreach() in home/....../public_html/vendor/magento/module-media-storage/Model/File/Validator/NotProtectedExtension.php on line 84"

issue with import option also.

    protected function _initProtectedFileExtensions(){
    if (!$this->_protectedFileExtensions) {
        $extensions = $this->getProtectedFileExtensions();
        if (is_string($extensions)) {
            $extensions = explode(',', $extensions);
        }
        foreach ($extensions as &$ext) {
            $ext = strtolower(trim($ext));
        }
        $this->_protectedFileExtensions = (array)$extensions;
    }
    return $this;
}

Does anyone have idea about this issue?

I used Magento 2.3.1, PHP 7.2

2 REPLIES 2

Re: getting error Warning: Invalid argument supplied for foreach()

The error occurs because in the foreach expects the parameters as array , but it got empty or string.

Rewrite the code as :

protected function _initProtectedFileExtensions(){
    if (!$this->_protectedFileExtensions) {
        $extensions = $this->getProtectedFileExtensions();
        if (is_string($extensions)) {
            $extensions = explode(',', $extensions);
        }
        if (is_array($extensions) && !empty($extensions)) {
            foreach ($extensions as &$ext) {
            $ext = strtolower(trim($ext));
            }
        }
        $this->_protectedFileExtensions = is_array($extensions) ? (array)$extensions : [];
    }
    return $this;
}

Re: getting error Warning: Invalid argument supplied for foreach()

protected function _initProtectedFileExtensions(){
if (!$this->_protectedFileExtensions) {
$extensions = $this->getProtectedFileExtensions();
if (is_string($extensions)) {
$extensions = explode(',', $extensions);
}
// check if don't array
if (!is_array($extensions) ) {
$extensions = [];
}
foreach ($extensions as &$ext) {
$ext = strtolower(trim($ext));
}
$this->_protectedFileExtensions = (array)$extensions;
}
return $this;
}
LitExtension - #1 Shopping Cart Migration Expert

LitExtension helps store owners and agencies migrate all important data from one eCommerce platform to another accurately, securely and at the highest speed.

Visit website: http://litextension.com/