0

I've following array of file extensions titled $aSupportedImages:

Array
(
    [0] => jpeg
    [1] => jpg
    [2] => gif
    [3] => png
)

I've another array titled $values as follows :

Array
(
    [vshare] => Array
        (
            [course_error.png] => Array
                (
                    [0] => https://www.filepicker.io/api/file/Y0n99udSqS6ZJWYeYcUA
                )

            [before_login.png] => Array
                (
                    [0] => https://www.filepicker.io/api/file/19FWbHh1QNGCo2OINxI6
                )

            [Sample_1.docx] => Array
                (
                    [0] => https://www.filepicker.io/api/file/INjMeEhCSjpZSfZJmQUb
                )

        )

)

Now you can see that each key in an array [vshare] is a file name. I want to check the extension of each of such file with the extensions present in an array $aSupportedImages. If any of the files have different extension than those present in the array $aSupportedImage the loop should get break and it should return false.

In above case for third file it should return false. As .docx is not present in an array $aSupportedImages

How should I do this? Please help me.

  • 3
    Have you tried anything? – Rizier123 Mar 02 '15 at 08:12
  • You will need to resolve the headers of those URIs to see what the resulting filename it. Have a look at https://stackoverflow.com/questions/1378915/header-only-retrieval-in-php-via-curl – Tom Mar 02 '15 at 08:17

3 Answers3

0

please try in_array

 foreach($vshareArray as $key => $value){
      if(in_array($key, $aSupportedImages)){
       echo "valid";
    }else{
     echo "not valid";
    }
    }
Tariq hussain
  • 614
  • 5
  • 11
  • While this may answer the question it’s always a good idea to put some text in your answer to explain what you're doing. Read [how to write a good answer](http://stackoverflow.com/help/how-to-answer). – Jørgen R Mar 02 '15 at 09:23
0

This should break if there's a file with unsupported extension

foreach($values['vshare'] as $file)
{
    if(!in_array(pathinfo($file, PATHINFO_EXTENSION), $aSupportedImages))
        break;
}
Phate01
  • 2,499
  • 2
  • 30
  • 55
0

try this:

<?php
      
  function get_ext($filename) {
  
    return strtolower(substr($filename, strrpos($filename, '.')));

  }

  $ext_authorized = array('.jpg', '.jpeg', '.png', '.gig');

  $values = array (
    'vshare' => array (
      'course_error.png' => array ('https://www.filepicker.io/api/file/Y0n99udSqS6ZJWYeYcUA'),
      'before_login.png' => array ('https://www.filepicker.io/api/file/19FWbHh1QNGCo2OINxI6'),
      'Sample_1.docx' => array ('https://www.filepicker.io/api/file/INjMeEhCSjpZSfZJmQUb')
    )
  );

  foreach($values['vshare'] as $key => $val) {
      
    if (in_array(get_ext($key), $ext_authorized)) {
  
      //do something

    } else {

      //do something

    }
    
  }
?> 
nekiala
  • 450
  • 9
  • 21