php 非法偏移类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2188456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal offset type
提问by BFTrick
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.
我在通过 php 上传文件时遇到问题。我在过程开始时检查文件类型,但出现错误。
This is the error I am getting:
这是我得到的错误:
Warning: Illegal offset type in /balblabla/DBfunctions.inc.php on line 183
警告:第 183 行 /balblabla/DBfunctions.inc.php 中的非法偏移类型
This is the printed out $_FILES var
这是打印出来的 $_FILES var
Array ( [Picture] => Array ( [name] => JPG.jpg [type] => image/jpeg [tmp_name] => /tmp/phpHlrNY8 [error] => 0 [size] => 192221 ) )
数组 ( [图片] => 数组 ( [名称] => JPG.jpg [类型] => 图像/jpeg [tmp_name] => /tmp/phpHlrNY8 [错误] => 0 [大小] => 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
if( checkFile( $_FILES['Picture'], array("image/jpeg") ) == true ){
//做东西
}// 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?
我已经在我自己的服务器上的数十个网站上使用了这段代码,所以我猜这是一些不同的配置选项。我如何修改我的代码以便它在这个不同的服务器上工作?
回答by SoapBox
You are passing an array, not a string/integer index to your checkFilefunction.
您正在向checkFile函数传递一个数组,而不是字符串/整数索引。
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:
更改 checkfile 以便它使用传入的数组进行检查,因此:
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.
任何一种改变都会奏效。
回答by user187291
In checkFile()replace $_FILES[$file]with $file. You're indexing $_FILESarray twice.
在checkFile()替换$_FILES[$file]用$file。你索引$_FILES数组两次。

