How would I upload an image and the following input text from the form to with the help php curl? How do I post both an image file and text attributes using formData? Is there anything else I should use? Is there a simpler way to achieve this? I want to post both the image and the text fields. I have been trying many different methods, but none have seemed to work for me. If anyone can explain it to me as well that would be greatly appreciated.
Here is my form -->
<form id="form" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="name" name="name" type="text" class="form-control">
</div>
<div class="form-group">
<input id="file" name="file" type="file" class="form-control-file">
</div>
<button id="button" class="btn btn-lg btn-primary">
<span>Send</span>
</button>
</form>
This is my javascript -->
var form = $('form')[0];
formData = new FormData(form);
$.ajax({
url: 'apiController',
data: formData,
type: 'POST',
contentType: false,
processData: false,
});
This is my apiController -->
$imageFile = $_POST['file'];
function sendData() {
$url = "Here I will have my server"
$curl = curl_init();
$contentType = 'multipart/form-data';
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $imageFile,
CURLOPT_HTTPHEADER => array(
$contentType;
),
));
curl_close($curl);
}