Jump to content


Photo

in_array problem.


  • Please log in to reply
8 replies to this topic

#1 Nexous

Nexous
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 13 August 2006 - 02:32 AM

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

<?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;
}
?>

I'm not sure how my instance is different from php.net/in_array 's example.

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 August 2006 - 03:15 AM

it means that the second parameter, $not_valid_exts, is not actually an array.

#3 Nexous

Nexous
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 13 August 2006 - 03:19 AM

<?php

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


#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 August 2006 - 03:20 AM

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.

#5 Nexous

Nexous
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 13 August 2006 - 03:29 AM

The file defines the array, then calls for '$not_valid_exts'
<?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) ) 
		{
//......
}
?>


#6 Nexous

Nexous
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 13 August 2006 - 04:07 AM

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=......

<?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.';
}
?>

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 '/'?

#7 Nexous

Nexous
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 14 August 2006 - 02:00 PM

*bump*

#8 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 14 August 2006 - 08:31 PM

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():

$extension = strrchr($filename, '.');

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.

#9 Nexous

Nexous
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 August 2006 - 01:04 AM

Thanks, I've got it all sorted out now.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users