1

I'm using ajax to post form data and upload files from client to server (php),

my problem comes when I have to get the file content in the server side

for Some reason, $_FILES variable seems to come empty no matter what I try. Here's the code I have so far...

<!-- html5 -->

    <form id="uploadImg" enctype="multipart/form-data">
    <label>Panorámica</label>
    <div class="custom-file">
    <input type="file" class="custom-file-input" id="panoramica" name="panoramica" >
    <label class="custom-file-label" for="panoramica">Seleccionar Archivo</label>
    </form>

//jquery

$('#uploadImg').on('submit', function(e){
    e.preventDefault();

        var url = "upfilephp.php";
        jQuery.ajax({
        url: url,
        type: "POST",
        data:  new FormData(this),
        contentype: false,
        cache: false,
        processData: false,
            success: function (data) {                      
                console.log(data)
            },
             error: function(err){
                console.log(err)
            }
        });




//php
<?php
    if(!empty($_FILES['panoramica']['name'])){
        echo 'no problem';          
    }else{
        echo 'img is empty';            
    }
?>

Any idea how to solve this or what I'm I doing wrong?

calljhon
  • 72
  • 2
  • 9
  • It looks like it should work. What does `var_dump($_FILES);` show? – Barmar Aug 23 '19 at 23:21
  • Will it work with very small files ? – Accountant م Aug 23 '19 at 23:47
  • same result, in case it cleares up something, this is how the request looks like (if I check the php file in network, dev mode): ------WebKitFormBoundaryndLLPf154vmZO8G7 Content-Disposition: form-data; name="panoramica"; filename="destiny.jpg" Content-Type: image/jpeg – calljhon Aug 26 '19 at 21:21

2 Answers2

0

Typo. change contenttype to contentType.Try this one

$('#uploadImg').on('submit', function(e){
    e.preventDefault();
    var url = "testupload.php";

    jQuery.ajax({
        url: url,
        type: "POST",
        data: new FormData(this),
        processData: false,
        cache: false,
        contentType: false,
        success: function(datas) {
            console.log(datas)

        },
        error: function(err){
            console.log(err)
        }
    });
});
</script>
marlex
  • 26
  • 1
  • 6
0

Check your php.ini:

file_uploads = On
Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30