0

I have a form with multiple inputs like this:

<div id="users">
<input type="text" name="title[]" required>
<input type="file" name="images[]">

<input type="text" name="title[]" required>
<input type="file" name="images[]">

<input type="text" name="title[]" required>
<input type="file" name="images[]">

</div>

<input type="button" value="add new user">
<input type="submit" value="submit">

the problem is that selecting image is optional and if the user doesn't select any image for any of titles the posted data would be like this:

title  = ["title1","title2","title3"]
images = [image1.jpg,image3.jpg]

the count of users is not fixed and user can add any desired number of titles and images by clicking on the add new user button. so "add new user" button will add a pair of these inputs to users div:

<input type="text" name="title[]" required>
<input type="file" name="images[]">

is there any way to detect if the file is selected or not? or to send a default value for not selected file inputs? I want to set null value for image if no file is selected, like this:

user1:{title="title1",image=image1.jpg}
user2:{title="title2",image=null}
user3:{title="title3",image=image3.jpg}
.
.
.
2
  • Assign names for them, images[one], images[two] Commented Dec 31, 2020 at 13:45
  • the count of the inputs is not fixed so I cant assign names like this Commented Jan 6, 2021 at 16:50

3 Answers 3

1

You can check if particular index is set for file:

var_dump($_POST['title'][0]);
if (isset($_FILES['images']['tmp_name'][0])) {
    var_dump($_FILES['images']['tmp_name'][0]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hard coding a specific index is not require, the browser will return the array either way, but correctly indexed, arrays in PHP usually start at 0 not 1
You are right. I edit the answer. I was not sure if empty file inputs are sent. But yes they are.
the problem is that if you leave image2 empty the input will be ignored and just two files would sent to server ex: [file1,file3] so I can't recognize that which file is set empty if the user leave the file1 empty then I have an array with two items again!
That's not right. Try dump $_FILES variable and see what is the structure. I test it to be sure and structure is as expected with every image input, even empty one. array(1) { ["images"]=>; array(5) { ["name"]=>; array(3) { [0]=>; string(0) "" [1]=>; string(0) "" [2]=>; string(0) "" } ...
in my case if I set one file from three optional file inputs, I get an array with length of 1.
0

All you have to do is process the 2 arrays and check there is a matching image with each title. If there is not a image for each title, put anything that you like in place of the image name

<?php
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  
    $output = [];

    foreach( $_POST['title'] as $idx => $title){
      $u = $idx+1;
      $output['user'.$u] = ['title'=>$title, 
                              'images'=> $_POST['images'][$idx] == '' ? 'NULL' : $_POST['images'][$idx]];
    }
    $j = json_encode($output);
    echo $j;
  }

RESULT

{
    "user1":{"title":"asss","images":"NULL"},
    "user2":{"title":"dd","images":"hammer.jpg"}
}

1 Comment

the problem is that, if user set file1 empty or file2 empty, the request would be an array with 1 item, its impossible to know which one is set to empty. in your example if the user set image1 and leave image2 empty then again I will have a array with 1 item. in both case images[1] = file , images[2]=null
0

Thank you everybody, Pavol Velky was right partially, Although the array size is less than the number of inputs, but the browser will set an index to the array, in example if you have two inputs and just select the second one, the array will be like : item:[1:{}] (if you select first one: item:[{}]) and you can check the order by the index number.

Comments

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.