Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal offset type

I am having trouble uploading a file through php. I check the file type at the beginning of the process and I get an error.

This is the error I am getting:

Warning: Illegal offset type in /balblabla/DBfunctions.inc.php on line 183

This is the printed out $_FILES var

Array ( [Picture] => Array ( [name] => JPG.jpg [type] => image/jpeg [tmp_name] => /tmp/phpHlrNY8 [error] => 0 [size] => 192221 ) )

Here is the segment of code I am using that is giving me issues:

function checkFile($file, $type)
{
    if( in_array($_FILES[$file]['type'], $type) ){    //   <---  LINE 183
        return true;
    }//if
    return false;
} // end checkFile()

This is the line of code that calls the function

if( checkFile( $_FILES['Picture'], array("image/jpeg") ) == true ){
//do stuff
}// end if

I have used this piece of code on dozens of websites on my own server so i am guessing that this is some different configuration option. How can i modify my code so that it works on this different server?

like image 209
BFTrick Avatar asked Feb 02 '10 23:02

BFTrick


2 Answers

You are passing an array, not a string/integer index to your checkFile function.

To fix this, make one of the following to changes:

Change checkfile so that it uses the array passed in to do the checking, thusly:

if( in_array($file['type'], $type) )

OR change the code that calls this function so that it passes the name of the file to use as an index rather than the file array, thusly:

if( checkFile('Picture', array("image/jpeg") ) == true )

Either change will work.

like image 86
SoapBox Avatar answered Sep 20 '22 09:09

SoapBox


In checkFile() replace $_FILES[$file] with $file. You're indexing $_FILES array twice.

like image 35
user187291 Avatar answered Sep 18 '22 09:09

user187291