0

I am trying to get the true value, but result always show false, how to fix this case?

$allow = array(
    "pdf"=>"application/pdf",
    "doc"=>"application/msword",
    "docx"=>"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "docx"=>"application/zip"
);

if (in_array("application/vnd.openxmlformats-officedocument.wordprocessingml.document",$allow)) {
    echo "true";
} else {
    echo "false";
}

In this case, I have two *.docx file but the mime type is different, how to allow both of them???

6
  • 3
    you can't define two array elements with same key, if you used same key for two array elements then latest one override to previous Commented Jan 10, 2017 at 7:04
  • i have two file docx with different mime, how to allow both of them? Commented Jan 10, 2017 at 7:10
  • @AbuAyyub : docx1 , docx2 ?? Commented Jan 10, 2017 at 7:11
  • 1
    It would be better if you create multi array for same key's e.g. , $allow = array( "pdf"=>"application/pdf", "doc"=>"application/msword", "docx"=>array("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/zip") ); Commented Jan 10, 2017 at 7:13
  • no... but test.docx the mime is "application/vnd.openxmlformats-officedocument.wordprocessingml.document" test2.docx the mime is "application/zip" Commented Jan 10, 2017 at 7:14

4 Answers 4

2

The problem is that the array contains two items with equal keys. Namely, "application/zip" overrides "application/vnd.openxmlformats-officedocument.wordprocessingml.document". The following is the output of print_r($allow);:

Array
(
    [pdf] => application/pdf
    [doc] => application/msword
    [docx] => application/zip
)

So you should specify different key for application/zip, e.g.:

$allow = array( "pdf" => "application/pdf",
  "doc" => "application/msword",
  "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  "zip" => "application/zip"
);
Sign up to request clarification or add additional context in comments.

2 Comments

i have two file docx with different mime, how to allow both of them?
@AbuAyyub, simply use different keys for the array items
1

This might help you please try the below solution. You can make docx as one key value and put the mime as comma separated.

 $allow = array(
    "pdf" => "application/pdf",
    "doc" => "application/msword",
    "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/zip",
);

$allownew = array_values($allow);


if (in_array("application/vnd.openxmlformats-officedocument.wordprocessingml.document", $allownew))
{
    echo "true";
}
else
{
    echo "false";
}

1 Comment

Very bad practice. Use array_values instead of foreach loop. Still not a recommended way. Use indexed array instead
1

I can see that keys are not important in your case. You can use indexed array instead of multidimensional array.

$allow = array("application/pdf",
               "application/msword",
               "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
               "application/zip");

If you really want to use multidimensional array then,

Use different keys for both mimes such as docx1, docx2.

$allow = array(
    "pdf"=>"application/pdf",
    "doc"=>"application/msword",
    "docx1"=>"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "docx2"=>"application/zip"
);

Comments

1

Maybe you should try it with a "flipped" version of the array:

$allow = array(
    "application/pdf"=>"pdf",
    "application/msword"=>"doc",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document"=>"docx",
    "application/zip"=>"docx"
  );

Now, of course, you will need to search the keys of the array, not the values like

$k="application/vnd.openxmlformats-officedocument.wordprocessingml.document";
If (array_key_exists($k,$array)) echo $array[$k]; // docx

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.