1

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";
}   
6
  • first check print_r($_FILES) Commented Nov 9, 2016 at 8:49
  • i checked alredy print_r($_FILES) but else part are executed.... Commented Nov 9, 2016 at 8:52
  • than use trim maybe some extra space here if(empty(trim($imgsfiles))) Commented Nov 9, 2016 at 8:53
  • 1
    So post the result of print_r($_FILES). Commented Nov 9, 2016 at 8:56
  • trim function also use but same else function are executed Commented Nov 9, 2016 at 8:57

1 Answer 1

3

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';
}
Sign up to request clarification or add additional context in comments.

1 Comment

@jeroen if there are X file fields then count($_FILES['editimagefile']['name']) will return X no matter file selected or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.