Jump to content

Archived

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

Nexous

in_array problem.

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.

Share this post


Link to post
Share on other sites
it means that the second parameter, $not_valid_exts, is not actually an array.

Share this post


Link to post
Share on other sites
[code]<?php

$not_valid_exts = array('.gif','.png','.jpg','.css','.htm','.html','.htaccess');
?>[/code]

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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 '/'?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.