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

COil
  • 6,187
  • 2
  • 40
  • 80
Abu Ayyub
  • 369
  • 3
  • 14
  • 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 – Ranjit Shinde Jan 10 '17 at 07:04
  • i have two file docx with different mime, how to allow both of them? – Abu Ayyub Jan 10 '17 at 07:10
  • @AbuAyyub : docx1 , docx2 ?? – Ravi Jan 10 '17 at 07: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") );` – Ranjit Shinde Jan 10 '17 at 07:13
  • no... but test.docx the mime is "application/vnd.openxmlformats-officedocument.wordprocessingml.document" test2.docx the mime is "application/zip" – Abu Ayyub Jan 10 '17 at 07:14
  • @Ranjith : In that case, he also need to check that whether key has array or not. All mimes in first level would be more feasible. – Ravi Jan 10 '17 at 07:16

4 Answers4

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"
);
Ruslan Osmanov
  • 17,894
  • 7
  • 38
  • 53
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"
);
Ravi
  • 5,913
  • 1
  • 20
  • 37
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";
}
craig
  • 269
  • 4
  • 14
  • Very bad practice. Use ***array_values*** instead of foreach loop. Still not a recommended way. ***Use indexed array instead*** – Ravi Jan 10 '17 at 07:40
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
Carsten Massmann
  • 16,701
  • 2
  • 16
  • 39