upload multiple files [duplicate]

1

How do I get the files in AJAX ?

    

ThisistheAJAXthatisworking,butIamnotpassingthefilesasdate,IamgettinginPHP

$('#btnenviarpdf').click(function(){varid=$('#modalADDPDF').data('id');$.post('estrutura/upaddpdf.php',{acao:'inserir',id:id},function(r){varm=jQuery.parseJSON(r);if(m.success){$('#modalADDPDF').modal('hide');toastr["success"](m.msg);           
            $("#listapdf").load(location.href + " #listapdf>*", "");    

           } else {

            toastr["error"](m.msg);
            $('#modalADDPDF').modal('hide');

           }             
        });
    });

This error occurs:

  

Notice: Undefined index: img in upaddpdf.php on line 14

PHP:

<?php
    include("../includes/config.php");

    if($_POST){

        $data = array('success' => '0',
                  'msg' => 'Ocorreu um erro, nada foi inserido!');

        $acao   = $_POST['acao'];
        $id     = $_POST['id'];

        IF($acao != ""){
            //INFO IMAGEM
            $file       = $_FILES['img'];  <<<=== linha 14
            $numFile    = count(array_filter($file['name']));

            //PASTA
            $folder     = 'arquivos/anexos';

Now I have done this, but the same problem continues.

JQUERY

$('#btnenviarpdf').click(function () {

    var id = $('#modalADDPDF').data('id');  

    var form_data = new FormData();
    var ins = document.getElementById('multiFiles').files.length;
    for (var x = 0; x < ins; x++) {
        form_data.append("files[]", document.getElementById('multiFiles').files[x]);
    }

    $.post('estrutura/upaddpdf.php',{acao:'inserir',id:id},function(r) { 

       var m = jQuery.parseJSON(r);        
       if (m.success) {


        $('#modalADDPDF').modal('hide');
        toastr["success"](m.msg);           
        $("#listapdf").load(location.href + " #listapdf>*", "");    

       } else {

        toastr["error"](m.msg);
        $('#modalADDPDF').modal('hide');

       }             
    });
});

PHP

<?php
include("../includes/config.php");

if($_POST){

    $data = array('success' => '0',
              'msg' => 'Ocorreu um erro, nada foi inserido!');

    $acao   = $_POST['acao'];
    $id     = $_POST['id'];

    IF($acao != ""){
        //INFO IMAGEM
        $file       = $_FILES['files'];
        $numFile    = count(array_filter($file['name']));

Form:

<form id="formulario" method="post" enctype="multipart/form-data" action="">
                        <input type="hidden" name="acaopdf" value="">
                        <input type="hidden" name="id" value="<?php echo $id?>">
                        <div class="file-field">                    

                            <div class="d-flex justify-content-center">
                                <div class="btn btn-info btn-rounded btn-sm">
                                    <span id="spa">Escolha o arquivo</span>                                         
                                    <input type="file" id="mulitplefileuploader" name="img[]" multiple>

                                </div>

                            </div>
                        <center><input class="btn btn-info btn-rounded btn-sm" id="btnenviarpdf" type="submit" value="Enviar" /></center>
                        </div>

                    </form>
    
asked by anonymous 20.02.2018 / 02:09

1 answer

0

Your Ajax does nothing but send {acao:'inserir',id:id} , ie you do not have PHP to receive the upload since you did not send anything, to send the files it is necessary to pass the FormData in the date field and set the processData as false

It will be necessary to pass the action and the id in the FormData itself:

//O mesmo que acao: 'inserir'
form_data.append("acao", "inserir");

//O mesmo que id: id
form_data.append("id", id);
  

You do not need JSON.parse, you can simply use dataType: "json"

Another interesting thing to use is .fail , because you can get errors on the server or even on the user's internet, with fail you can tell if something has failed in this situation:

.fail(function (xhr, textErr) {
    toastr["error"](textErr);
    $('#modalADDPDF').modal('hide');
});

It should look like this:

var id = $('#modalADDPDF').data('id');  

var form_data = new FormData();
var ins = document.getElementById('multiFiles').files.length;

for (var x = 0; x < ins; x++) {
    form_data.append("files[]", document.getElementById('multiFiles').files[x]);
}

//O mesmo que acao: 'inserir'
form_data.append("acao", "inserir");

//O mesmo que id: id
form_data.append("id", id);

$.ajax({
  type: "POST",
  url: 'estrutura/upaddpdf.php',
  data: form_data, //Passa o forma data
  dataType: "json",//Força o tipo json na resposta
  processData: false // <--- false
}).done(function(r) {
   if (m.success) {


    $('#modalADDPDF').modal('hide');
    toastr["success"](m.msg);           
    $("#listapdf").load(location.href + " #listapdf>*", "");    

   } else {

    toastr["error"](m.msg);
    $('#modalADDPDF').modal('hide');

   }     
}).fail(function (xhr, textErr) {
    toastr["error"](textErr);
    $('#modalADDPDF').modal('hide');
});
    
20.02.2018 / 13:06