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?
$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..