0

Hello I'm having issue with php in_array function. I want to check the uploaded files extension whether it meets my requirement or not, and for that I'm using following code.

$imgExtension = explode(".",$imageName);
$allowed_extension = array("png","jpg","jpeg","gif");
echo $end = end($imgExtension);

if(in_array(trim($end),$allowed_extension)){
    echo "File is correct";

}else{
    echo "File is incorrect";

}

The issue that I'm facing is the in_array function, the uploaded file's extension is jpg,png and jpeg, yet the in_array condition turns false, don't know why...

but instead of $end variable, if I pass any string like "jpg" or "png" in in_array functions parameter, then it works...

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • `var_dump($end);` verify it contains what you think before going into `in_array`. – aynber Nov 25 '19 at 13:34
  • Does this answer your question? [php check file extension in upload form](https://stackoverflow.com/questions/10456113/php-check-file-extension-in-upload-form) – 4givN Nov 25 '19 at 13:36
  • 1
    This seems to be working fine for me. [See here](https://3v4l.org/HTvlt) – Patrick Q Nov 25 '19 at 13:36
  • This will help: https://stackoverflow.com/questions/10456113/php-check-file-extension-in-upload-form – 4givN Nov 25 '19 at 13:36
  • Since your array of file extensions are all lower case you need to make sure that the extension of the uploaded files is also all lower case too. – Dave Nov 25 '19 at 15:57

0 Answers0