Recover dynamic input file via php

Asked

Viewed 147 times

0

I create dynamic field blocks, with two inputs and a file, in this way:

$('#input_nova').click(function()
{
    var total_item = $(".input_item").length ;
    $('#input_container').append(
        '<div class="col-md-6 col-xs-12 input_item">\
            <h5>Título</h5>\
            <input type="text" name="imovel_input_titulo['+total_item+']" id=""/>\
            <h5>Imagem</h5>\
            <input type="file" name="imovel_input_imagem['+total_item+']" id=""/>\
            <h5>Descrição</h5>\
            <textarea name="imovel_input_descricao['+total_item+']" ></textarea>\
        </div>'
    );
});

No post via the foreach the data of inputs are checked without problem:

$input_titulo       = $_POST['imovel_input_titulo'];
$input_descricao    = $_POST['imovel_input_descricao'];
$input_imagem       = $_FILES['imovel_input_imagem'];
$total              = count($input_titulo);

for($i = 0; $i < $total; $i++)
{
    printf("Título : %s <br />", $input_titulo[$i]);
    printf("Descrição : %s <br />", $input_descricao[$i]);
    printf("Imagem : %s <br />", $input_imagem['imovel_input_imagem']['name'][$i]);
    echo '<br /><hr />';
}

But, in the input file, returns this error:

Notice: Undefined index: imovel_input_imagem

  • Your form has enctype="multipart/form-data"?

  • Opa, has yes, I have other fields that are not image dynamics, and are normally upados

  • Sending is via Ajax?

  • Via post normal

  • Something’s wrong, make a var_dump($_FILES);, I’m sure you made some mess, post the result of the var_dump.

  • Sent two images, the return was this. Array ( [name] => Array ( [0] => 335114.jpg [1] => 335114.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg ) [tmp_name] => Array ( [0] => D:\Desenvolvimento\tmp\php3B67.tmp [1] => D:\Desenvolvimento\tmp\php3B68.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 147631 [1] => 147631 ) ) &#xA;

  • You didn’t var_dump($_FILES);? Did you change the code to something else? Because that answer looks like you did something like var_dump($_FILES['imovel_input_imagem']);, if _FILES had been correct it would have returned the input name added in var_dump. Check pq vc did something wrong in this debug.

  • dump: `array(1) { ["imovel_input_image"]=> array(5) { ["name"]=> array(2) { [0]=> string(29) "1_IqAaP4KoJfaxTEOlPHEbA.jpeg" [1]=> string(10) "335114.jpg" } ["type"]=> array(2) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" } ["tmp_name"]=> array(2) { [0]=> string(47) "D: tmp development phpF886.tmp" [1]=> string(47) "D: tmp development phpF887.tmp" } ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } ["size"]=> (2) { [0]=> int(202992) [1]=> int(147631) } } }

Show 4 more comments

2 answers

0

Solved

    for($i = 0; $i < $total; $i++)
    {
        printf("Título : %s <br />", $input_titulo[$i]);
        printf("Descrição : %s <br />", $input_descricao[$i]);
        printf("Imagem : %s <br />", $_FILES['imovel_input_imagem']['name'][$i]);
        echo '<br /><hr />';
    }

-1

From what I noticed missed the index in the picture field, try to add:

 for($i = 0; $i < $total; $i++)
        {
            printf("Título : %s <br />", $input_titulo[$i]);
            printf("Descrição : %s <br />", $input_descricao[$i]);
            printf("Imagem : %s <br />", $input_imagem[$i]['imovel_input_imagem']['name'][$i]);
            echo '<br /><hr />';
        }
  • Notice: Undefined offset: on the line of print of the image

  • give a var_dump in $_POST and see if it’s really returning @sNniffer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.