title: PHP cURL file upload to JSP script fails. Same file uploaded via browser works
body: <p>Using PHP/cURL I log into a web site and attempt to upload a file. It returns the error message, "Error: File upload not processed. Please ensure that an applicable excel file in the specified template format is selected for upload."  This error is different from the message I get if I upload from a browser and give it a file of the wrong format (like a .jpg).</p>
  
  <p>If I upload the same excel file through Firefox it works and is accepted by the server.</p>
  
  <p>So how can I make this work through PHP/cURL the way it does with Firefox?</p>
  
  <p>I tried the same upload to my own PHP script and it also works fine.  I also reviewed some relevant questions here and tried various things, none of which made any difference.  I tried with and without the MIME type also with and without options CURLOPT_SSL_VERIFYPEER and CURLOPT_HTTPHEADER.</p>
  
  <p>The owner of the web site was not particularly helpful, only telling me that others are doing it.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>$url = 'https://www.example.com/uploadController_u.jsp';
  
  curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie_file );
  curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookie_file );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
  curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
  curl_setopt( $ch, CURLOPT_FAILONERROR, true );
  curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
  curl_setopt( $ch, CURLOPT_REFERER, $curl_referer );
  curl_setopt( $ch, CURLOPT_URL, $url );
  curl_setopt( $ch, CURLOPT_VERBOSE, true );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); // Added based on another StackOverflow question
  curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Expect:') ); // Added based on another StackOverflow question
  
  // Tried with and without the MIME type
  $post_vars = array( 
  'file' =&gt; "@/home/someuser/private/eapis-test/mx/Template-test.xls;type=application/vnd.ms-excel",
  'submit' =&gt; 'Upload file'  );
  
  curl_setopt( $ch, CURLOPT_POST, true );
  curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_vars );
  $response = curl_exec( $ch );
  </code></pre>
  
  <p>Below is the form I am posting to. I did not find any references to form1 in the javascript.</p>
  
  <pre><code>&lt;table border="0"  cellspacing="0" cellpadding="0"&gt;
      &lt;form action="uploadController_u.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1"&gt;
      &lt;tr&gt;
          &lt;td align="left"&gt;
              &lt;p&gt;
                  &lt;h2&gt;Upload Flight:&lt;/h2&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;b style="font-size: 90%;"&gt;To add a new flight from spreadsheet, &lt;/b&gt;
          &lt;/td&gt;
          &lt;td align="left"&gt;
              &lt;input name="file" type="file" id="file" align="left" size="36"&gt;
          &lt;td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
          &lt;/td&gt;
          &lt;td align="left"&gt;
              &lt;p&gt;
                  &lt;input type="submit" name="Submit" value="Upload file"/&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/form&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I tried these headers to better simulate a browser, but to no avail:</p>
  
  <pre><code>$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
  $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header[] = "Cache-Control: max-age=0";
  $header[] = "Connection: keep-alive";
  $header[] = "Keep-Alive: 300";
  $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $header[] = "Accept-Language: en-us,en;q=0.5";
  $header[] = "Pragma: ";
  curl_setopt( $ch, CURLOPT_HTTPHEADER, $header ); 
  </code></pre>