Jump to content

PHP cURL file upload to JSP script fails. Same file uploaded via browser works


geomouchet

Recommended Posts

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).

If I upload the same excel file through Firefox it works and is accepted by the server.

 

So how can I make this work through PHP/cURL the way it does with Firefox?

 

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.

 

I posted this same question to StackOverflow, but no one there was able to help.

 

The owner of the web site was not particularly helpful, only telling me that others are doing it.

Below is my 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' => "@/home/someuser/private/eapis-test/mx/Template-test.xls;type=application/vnd.ms-excel",
'submit' => 'Upload file' );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_vars );
$response = curl_exec( $ch );

Below is the form I am posting to. I did not find any references to form1 in the javascript.

<table border="0" cellspacing="0" cellpadding="0">
<form action="uploadController_u.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<tr>
<td align="left">
<p>
<h2>Upload Flight:</h2>
</p>
</td>
</tr>
<tr>
<td>
<b style="font-size: 90%;">To add a new flight from spreadsheet, </b>
</td>
<td align="left">
<input name="file" type="file" id="file" align="left" size="36">
<td>
</tr>
<tr>
<td>
</td>
<td align="left">
<p>
<input type="submit" name="Submit" value="Upload file"/>
</p>
</td>
</tr>
</form>
</table>

I tried these headers to better simulate a browser, but to no avail:

$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 );

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.