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
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
Share on other sites

The file defines the array, then calls for '$not_valid_exts'
[code]<?php
//$file_ext is defined above..
$not_valid_exts = array('.gif','.png','.jpg','.css','.htm','.html','.htaccess');
if (!in_array($file_ext, $not_valid_exts) )
{
//......
}
?>[/code]
Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.