Jump to content

in_array problem.


Nexous

Recommended Posts

Warning: in_array(): Wrong datatype for second argument in /home/rhuser/public_html/resource/inc/rhu.php on line 67

[code]<?php
// $file is defined and correct.
// $file_ext is defined and correct.
// $not_valid_exts is an array.
if (!in_array($file_ext, $not_valid_exts) )
{
return $file;
}
?>
[/code]

I'm not sure how my instance is different from php.net/in_array 's example.
Link to comment
https://forums.phpfreaks.com/topic/17378-in_array-problem/
Share on other sites

are you putting that IMMEDIATELY before the in_array() statement?  it's possible that you're defining that in the global scope, and then trying to use it within a function.  this is unallowed unless you use $GLOBALS['not_valid_exts'] or you pass it to the function manually.
Link to comment
https://forums.phpfreaks.com/topic/17378-in_array-problem/#findComment-73927
Share on other sites

To make it much simplier, I'll explain what I'm trying to do.
Don't worry about security features.

I want to check valid file extensions..
The file is recieved from index.php?file=index.php
So, I want to check ?file=......

[code]
<?php
$file = $_GET['file'];
$valid = array("bad", "worse");
if(in_array($file, $valid)){
echo 'The File is bad, oh noes!';
}
else {
echo 'We are good, the file is allowed.';
}
?>[/code]

Now to think of it, would I have to split it up? say that the link is ?file=bad/horid.php
Would it still find bad in it, or would I have to split the '/'?
Link to comment
https://forums.phpfreaks.com/topic/17378-in_array-problem/#findComment-73939
Share on other sites

that looks valid to me, so i don't see what could be causing the parse error.  are you still getting the error?

for the record, you will not be checking the extension against the array, you will be checking the entire filename.  to strip the outermost extension from the filename, use strrchr():

[code]$extension = strrchr($filename, '.');[/code]

this will INCLUDE the period in the extension.  either add periods to each extension you want to check for (as your are currently), or strip the period from the extension you are taking from the filename with substr().

as for your original error, try running print_r($not_valid_exts) before running the if() that contains the in_array() function.
Link to comment
https://forums.phpfreaks.com/topic/17378-in_array-problem/#findComment-74744
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.