title: how to send multipart request using php
body: <p>I am uploading image on my signup page  which is submitted by ajax  and is send to ajaxcall.php where i add some headers to the request and send it to the register api  which register the user</p>
  
  <p>issue : i am receiving the $_POST and $_FILE in ajaxcall.php but on sending the curl request with the same parameters and few headers are added  i do not recieve the post params on the server side</p>
  
  <p>Below is the  code for creating  content deposition </p>
  
  <pre><code> function genereatePostFileVars($file,$rem=array(),$boundary)
  {
      $postvar="";
  
      foreach($_POST as $key=&gt;$val)
      {
          if($key!='endpoint')
          {
              if(!in_array($key,$rem))
              {
                  if($key != 'uname')
                  {
                  $postvar.="\r\n$boundary\r\nContent-Disposition: form-data; name=\"$key\"\r\n\r\n$val";
                  }
                  else
                  {
                      $postvar.="\r\n$boundary\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n$val";
                  }
              }
          }
  
      }
      if(!empty($_FILES))
      {
          $filedata=array();
          if(count($_FILES[$file]['name'])&gt;1)
          {
              for($i=0;$i&lt;count($_FILES[$file]['name']);$i++)
              {
                  $fst='file'.($i+1);
                  $filedata[$fst]['name']=$_FILES[$file]['name'][$i];
                  $filedata[$fst]['error']=$_FILES[$file]['error'][$i];
                  $filedata[$fst]['tmp_name']=$_FILES[$file]['tmp_name'][$i];
                  $filedata[$fst]['size']=$_FILES[$file]['size'][$i];
              }
          }
          else{
              $filedata['file']=$_FILES[$file];
  ;
          }
          foreach($filedata as $name=&gt;$value){
  
              $postvar.="\r\n".$boundary."\r\nContent-Disposition: form-data; name=\"$name\"; filename=\"".$value['name']."\"\r\nContent-Type:".$value['type']."\r\n";
          }
      }
      $postvar.="\r\n$boundary--";
      return $postvar;
  }
  </code></pre>
  
  <p>Output for above code:</p>
  
  <pre><code>-----1465984203238231624
  Content-Disposition: form-data; name="name"
  
  himanshi
  -----1465984203238231624
  Content-Disposition: form-data; name="username"
  
  wvhxc
  -----1465984203238231624
  Content-Disposition: form-data; name="email"
  
  wvhxc@gmail.com
  -----1465984203238231624
  Content-Disposition: form-data; name="password"
  
  vdschh
  -----1465984203238231624
  Content-Disposition: form-data; name="file"; filename="10940545_822721561108202_6765172210735079451_n.jpg"
  Content-Type:image/jpeg
  
  -----1465984203238231624--
  </code></pre>
  
  <p>Headers that are send:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Accept:application/json,image/*
      [1] =&gt; Expect: 
      [2] =&gt; authToken: 72b5a44d3e9c50fc81dd196d75a1b375
      [3] =&gt; timestamp: 2016-06-15 09:50:03
      [4] =&gt; content-type: multipart/form-data; boundary=-----1465984203238231624
      [5] =&gt; deviceType:Web
      [6] =&gt; apiVersion:v0.1
  )
  </code></pre>
  
  <p>Response i get:</p>
  
  <pre><code>HTTP/1.1 400 Bad Request
  Date: Wed, 15 Jun 2016 09:50:03 GMT
  Server: Apache/2.2.29 (Unix) mod_wsgi/3.5 Python/2.7.10 PHP/5.6.10 mod_ssl/2.2.29 OpenSSL/0.9.8zh DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.22.0
  X-Powered-By: PHP/5.6.10
  Set-Cookie: PHPSESSID=5f784328aa7571352c1ba2e127a96ef6; path=/
  Expires: Thu, 19 Nov 1981 08:52:00 GMT
  Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
  Pragma: no-cache
  X-Content-Type-Options: nosniff
  Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
  Access-Control-Allow-Headers: Authorization
  Allow: POST
  Content-Length: 80
  Connection: close
  Content-Type: application/json; charset=UTF-8
  
  {"status":false,"message":"Username or email is required for signup.","data":[]}
  </code></pre>
  
  <p>Please let me know what i am doing wrong,Thanks in advance</p>