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.
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;
}
please try in_array
foreach($vshareArray as $key => $value){
if(in_array($key, $aSupportedImages)){
echo "valid";
}else{
echo "not valid";
}
}
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
}
}
?>
</div>