0

Hi everybody, I'm confronting a really weird behaviour while uploading files to my php server using Ajax/jQuery.

Form: HTML

<form class="general_form" id="winner_form" >

    //upload profile picture
    <label for="profile_pic">Profile picture</label>
    <input type="file" class="file" name="profile_pic" accept="image/*" required><br><br>

    //upload multiple pictures
    <label for="pictures">Select Multiple Pictures</label>
    <input type="file" class="file" name="pictures[]" accept="image/*" multiple><br><br>

    //submit
    <button type="submit"> Submit</button>
</form>

Form: JS

    $(document).ready(function() {
    $(".general_form").submit(function(e) {
        //grab all form data  
    
        var data = new FormData($('#winner_form')[0]);
        e.preventDefault();
    
            $.ajax({
                url:          'upload.php',
                type:         'POST',
                datatype:     "html",
                data:         data,
                cache:        false,
                contentType:  false,
                processData:  false,
                success: function(data){
                    if(data.error)
                        alert("ERROR");
                    else
                        document.write(data);
                },
                error: function(data) {
                    alert("ERROR");
                    document.write(data);
                }
            });
    
            e.preventDefault();
    });
    
    });

Each time I print $_REQUEST & $_FILES content my uploaded files change location

first submit

$_REQUEST = 
array(3) {
string(11) "images.jpeg" 
  ["pictures"]=>
  array(2) {
    [0]=>
    string(8) "pic1.png"
    [1]=>
    string(8) "pic2.png"
  }
}

}
$_FILES = 
array(0) {}

second submit

 $_REQUEST = 
  array(2) {
 ["profile_pic"]=>
  array(5) {
    ["name"]=>
    string(11) "images.jpeg"
    ["type"]=>        ..
    ["tmp_name"]=>    ..
    ["error"]=>       ..
    ["size"]=>        ..
  }
  $_FILES = 
  ["pictures"]=>
  array(5) {
    ["name"]=>
    array(2) {
      [0]=>
      string(8) "pic1.png"
      [1]=>
      string(8) "pic2.png"
    }
    ["type"]=>    ..
    }
    ["tmp_name"]=> ..
    }
    ["error"]=>    ..
    }
    ["size"]=>     ..
    }
  }
}

Thanks a lot for all your help

Community
  • 1
  • 1
eldar
  • 195
  • 2
  • 12

0 Answers0