2

So at the moment I have the following two arrays:

Array ( [0] => image/jpeg [1] => image/png [2] => image/psd ) 
Array ( [0] => png [1] => jpeg [2] => jpg [3] => zip [4] => mov ) 

The top array is the $_GET data that is being forwarded, which are uploaded file types. The second array is a comma list of allowed file types. Basically, what I am trying to achieve is to check if each item in the array is an allowed file type and if not to return an error message.

My code:

$fileTypes = $_GET['data']; // Get the JSON file types

$fileTypes = json_decode($fileTypes); // Decode the data

$allowedTypes = "png,jpeg,jpg,zip,mov"; // List of allowed file types
$allowedTypes = explode(",", $allowedTypes); // Explode the list to an array

// For each file type in the array
foreach ($fileTypes as $fileType) {

foreach($allowedTypes as $allowedType) { 

$pos = strpos($fileType, $allowedType); // Check if the allowed type is contained in the file type

if($pos == FALSE) { echo "False"; } else { echo "True"; }

}

Now the issue is, this returns the following:

False, True, False, False, False (Overall will still equate to False, even though it had a true)
True, False, False, False, False (Overall will still equate to False, even though it had a true)
False, False, False, False, False (Overall will equate to false which it should as it has no trues)

Now as you can see, the code is working in the fact it is picking up when the state is true and false but I need to evaluate it overall. So if each line has a true in, then the overall state is true, however if one of the lines, like the last one contains all falses then the overall condition becomes false.

Not sure if that made any sense?

5
  • add a variable $state = true; on top of your foreach loop, and when you detect a false, set $state to false. After the loop is done $state is either true if all are true, or false if one or more are false. Also, if 1 false result is equal to a false return in the end, you might aswel break the loops as soon as a result becomes false.. Commented Mar 30, 2013 at 20:09
  • Surely this won't work though cause each time it outputs trues as well as falses? Commented Mar 30, 2013 at 20:10
  • Also, why not make $allowedTypes = array('png','jpeg','jpg','zip','mov'); then you don't need to run explode. Commented Mar 30, 2013 at 20:11
  • Do you need the out of "True" and "False"? Or is it just to see what is happening? I guess I missed the point of what you need. Commented Mar 30, 2013 at 20:12
  • Kind of got where you were coming from but I needed it in the opposite way. My new code is below as an answer. Seems to work. Commented Mar 30, 2013 at 20:16

3 Answers 3

2

This seems to work with a suggestion from PENDO.

// For each file type in the array
$state = TRUE;

foreach ($fileTypes as $fileType) {

foreach($allowedTypes as $allowedType) { 

$pos = strpos($fileType, $allowedType); // Check if the allowed type is contained in the file type

if($pos === FALSE) { $state = FALSE; } else { $state = TRUE; break; }

}

}

echo $state;
Sign up to request clarification or add additional context in comments.

Comments

1

zero is also false, so that, in the case when strpos finds the string at position zero it will be false: try using identical operator ===

if($pos === FALSE) 

Comments

0
function trueOrFalse($fileTypes, $allowedTypes)
{
  foreach($fileTypes as $fileType) {
    foreach($allowedTypes as $allowedType) {

      $pos = strpos($fileType, $allowedType);
      if($pos == FALSE) return false;
    }
  }
  return true;
}

Maybe this helps. As soon as a false $pos is detected, the function stops and returns false. Otherwise, it'll execute the function till the end, and if all $pos checks are true, returns true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.