Upload Multiple jQuery PHP Files

Asked

Viewed 881 times

1

I would like to send multiple files via post using jQuery, I tested several codes and could not, I would like you to help me.

I have this form:

HTML - index.html

<div class="arquivo" style="display: none;">
   <form name="form-file" id="form-file" method="post" enctype="multipart/form-data">
        <input type="file" multiple id="userfile" name="userfile[]" />
   </form>
</div>

Javascript - send.js

$('#userfile').on('change', function (event) {
    var data = new FormData(), file = [];
    var files = event.target.files;
    $.each(files, function(key, val) {
        file[key] = val;
    });

    data.append('file', file);
    $.ajax({

        url: 'upload1.php',
        type: 'post',
        data:  data,
        contentType: false,
        cache: false,
        processData: false,
        beforeSend: carregando,
        success: function (result) {
            // alert('Result: '+result);
            $('#uploaded-files').html(result);
            // sucesso('Arquivos enviados com sucesso!');
        }
    });
});

PHP

<?php
$path="arquivos/"; //server path
$result = '';
if(isset($_FILES['userfile']['name'][0])){
    //echo $_FILES['userfile']['name'][0];
    foreach ($_FILES['userfile']['name'] as $key => $file){
        $file_name = 'arquivos/'.$_FILES['userfile']['name'][$key];
        if(move_uploaded_file($_FILES['userfile']['tmp_name'][$key], $file_name)){
            // $result .= "<div class='image'><img src='${file_name}'></div>";
            $result .= "${file_name}<br />";
        }
    }

}else{
    $result =  "Nada";
}
?>

But in jQuery only returns "Nothing".

This same PHP code is used in Drag and Drop mode and works

Thank you in advance.

  • make a var_dump($_FILES); and tell us what returns (if it returns)

  • only returned array( )

1 answer

1


HTML - index.html

 <div>
        <form method="post" enctype="multipart/form-data">
            Selecione um ficheiro:
            <input type="file" name="fileToUpload[]" multiple accept="application/pdf">
            <input type="submit" value="Upload Image" name="submit" id="myFile" >
        </form>
    </div>   

Javascript

$(document).ready(function () {//
           $("form").submit(function(evt){
               var formData = new FormData($(this)[0]);
                $.ajax({
                    url: 'php/upload.php',
                    type: 'post',
                    data: formData,
                    contentType: false,
                    cache: false,
                    processData: false,                        
                    success: function (result) {
                        alert('Result: ' + result);
                    }
                });

            });

PHP

<?php
define('ROOTPATH', dirname(__DIR__) . '\\upload\\');
$result = '';
$property_images = $_FILES['fileToUpload']['name'];
if (!empty($property_images)) {
    for ($up = 0; $up < count($property_images); $up++) {
        if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'][$up], ROOTPATH . $_FILES['fileToUpload']['name'][$up])) {
            echo $result = "OK";
        }
    }
} else {
    echo $result = "Nada";
}
?>
  • was that very, and also in my form lack theenctype="multipart/form-data"

Browser other questions tagged

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