I am trying to send json object along with a file in ajax call as follows
Javascript
$('#btn').on('click', function(){
var file_data = $('#imgFile').prop('files')[0];
var form_data = new FormData();
let jsonObj = {
'label1':'value1'
};
form_data.append('file', file_data);
form_data.append('json', jsonObj);//json object which I am trying to send
$.ajax({
url: 'uploader.php',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
console.log(php_script_response);
}
});
});
and in PHP I am able to retrieve the file sent in ajax call successfully but I dont know how to access the json object sent along with that file
PHP
<?php
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
move_uploaded_file($_FILES['file']['tmp_name'], 'uploadedFiles/1.' . $file_ext);
echo $_POST['json'];
}
please let me know how to retrive the json object in php