Hello everyone!

Interested in this solution puzzles. There is such a HTML code

<input type="text" name="comp" placeholder="Comp" /><br> <input type="text" name="name[]" placeholder="name1" /><br> <input type="text" name="desc[]" placeholder="desc1" /><br> <input type="file" name="files[]" multiple /><br> <input type="text" name="name[]" placeholder="name2" /><br> <input type="text" name="desc[]" placeholder="desc2" /><br> <input type="file" name="files[]" multiple /><br> 

How to create an array with the names of the files that were selected in the first or second input. There is a script almost working, but it has a defect that instead of the file name there is a hieroglyph

  $this_is_it = array(); $post = array_values($_POST); for($j = 0; $j < count($post[1]); $j++){ for($u = 0; $u < count($post[3]); $u++){ $this_is_it['order_' . ($j + 1)] = array( 'name' => $post[1][$j], 'desc' => $post[2][$j], 'attach' => $post[3][$j] ); } } 

It displays with this:

 array(2) { ["order_1"]=> array(3) { ["name"]=> string(4) "lala" ["desc"]=> string(7) "lalalal" ["attach"]=> string(1) " " } ["order_2"]=> array(3) { ["name"]=> string(5) "name2" ["desc"]=> string(5) "desc2" ["attach"]=> string(1) " " } } 

What to do, how to be?

    1 answer 1

    The code snippet is correct, but it does not take into account the multiple choice of files. In the course of the discussion, it turned out that the blocks will be added dynamically, here’s the solution (as a separate file

     <?php for ($i=1;$i<=$_POST['quantity'];$i++) { $orders['order_'.$i]['name']=$_POST['name'.$i]; $orders['order_'.$i]['desc']=$_POST['desc'.$i]; $orders['order_'.$i]['files']=$_POST['files'.$i]; } //print_r($orders); ?> <form method="post"> <input type="text" name="comp" placeholder="Comp" /><br> <input type="text" name="name1" placeholder="name1" /><br> <input type="text" name="desc1" placeholder="desc1" /><br> <input type="file" name="files1[]" multiple /><br> <input type="text" name="name2" placeholder="name2" /><br> <input type="text" name="desc2" placeholder="desc2" /><br> <input type="file" name="files2[]" multiple /><br> <input type="hidden" name="quantity" value="2" /><br> 


    Upon the JS event, another data input block is added and 1, etc. is added to the input type = "hidden" name = "quantity" value = "

    • Thanks for the answer, but unfortunately I know it and this is not exactly what I need. You see, I need to get this design so that when the form is sent, the files from the first input are attached to the first 2 inpits, and accordingly the files from the second to the second input, and moreover, these input can be added as much as you like. That is the difficulty, if you know the answer to my question, please give me an answer to it. - B. Aleksei
    • So they are attached, ["attach"] in what form is needed, provided that there are several files? Processing a form is just kind of weird, it would not be easier to break it down by name and process everything. and this multiple means that you can pick up several files, do you need a few or 1 in each form? - Alexey Shatrov
    • See, in your case, if 2 files are selected, then only one file is transferred to the array. And your code, albeit weird, but I have prntscr.com/ecpou6 - so look at the file encoding, it should be UTF-8 without BOM - Alexey Shatrov
    • It would be generally excellent if in ["attach"] I would see the names of the files preferably through ",", but I myself can do it. Just through var_dump, I see a picture with a hieroglyph and not the file names, but about the form, I am now developing this system and did everything for a squatter, everything will be fine on my normal website. I just would like to get a civilized answer to my question, because I myself can not think of anything, my head just boils. - B. Aleksei
    • Everything is working fine for me, you have the file encoding - as I indicated above? - Alexey Shatrov