0

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);
}
Nick55
  • 21
  • 1
  • 5
  • This question has been asked before https://stackoverflow.com/a/21927891/156811 – Havenard Nov 25 '18 at 21:08
  • Possible duplicate of [What is the right way to POST multipart/form-data using curl?](https://stackoverflow.com/questions/19116016/what-is-the-right-way-to-post-multipart-form-data-using-curl) – But those new buttons though.. Nov 25 '18 at 21:08
  • Also just to point out a few problems, this is not how you set headers in cURL, `$_POST` is not where you will find the file data, also just giving the file name to postfields is not going to get anything done, you are not telling cURL what you want. – Havenard Nov 25 '18 at 21:10
  • I have already tried this solution @Havenard. I was not able to get it working. Any other suggestions? – Nick55 Nov 25 '18 at 21:12
  • Evidently you haven't, you're still doing everything wrong. Use the code from that answer I linked. – Havenard Nov 25 '18 at 21:13
  • Possible duplicate of [Posting raw image data as multipart/form-data in curl](https://stackoverflow.com/questions/21905942/posting-raw-image-data-as-multipart-form-data-in-curl) – hanshenrik Nov 25 '18 at 22:37
  • @billynoah that's how to do it with the curl cli program, this guy want to do it with the php curl_ api, it's quite a bit different from doing it with curl cli. – hanshenrik Nov 25 '18 at 22:38
  • @hanshenrik - ok - at the moment when I vtc someone else had already tagged that question and it looked close enough to me. my mistake, and thanks for finding a more appropriate dupe – But those new buttons though.. Nov 25 '18 at 23:15

0 Answers0