Aravinth E Aravinth E - 26 days ago 11
PHP Question

how to check condition php file input image array value is empty or not empty

Array name value is empty but print else condition. how to solve this.

this is my html file input code for image.

<input id="editimagefile" name="editimagefile[]" type="file" multiple=true>


this is my php value assigned code

$imgsfiles = $_FILES['editimagefile']['name'];

if(empty($imgsfiles))
{
echo "empty";
}
else
{
echo "value assigned";
}

Answer

Here name="editimagefile[]"is array field thus $_FILES['editimagefile']['name'] is an array too. Count function will return greater then 0 and it is not empty array but contains empty element.

So if you are sure you have more then one element you can use following:

if(in_array("", $_FILES['editimagefile']['name'])){
      echo 'contains empty elements';
}else{
      echo 'correct';
}

if only one element then you can use following:

if (empty($_FILES['editimagefile']['name'][0])) { 
    echo 'contains empty elements';
}else{
    echo 'correct';
}
Comments