使用PHP/cURL我登录到网站并尝试上载文件。它会返回错误消息“错误:未处理文件上传,请确保选择指定模板格式的适用excel文件进行上载。”如果我从浏览器上传并为其提供了错误格式的文件(如.jpg),则此错误与我获得的消息不同。将PHP cURL文件上载到JSP脚本失败。通过浏览器上传的相同文件工作
如果我通过Firefox上传相同的excel文件,它可以工作并被服务器接受。
那么我如何通过PHP/cURL这种方式使用Firefox的方式来完成这项工作?
我尝试了相同的上传到我自己的PHP脚本,它也正常工作。我也在这里回顾了一些相关的问题,并尝试了各种各样的东西,但没有一个有所作为。我尝试过使用和不使用MIME类型,也有和没有选项CURLOPT_SSL_VERIFYPEER和CURLOPT_HTTPHEADER。
该网站的所有者并不是特别有用,只告诉我,其他人正在这样做。
下面是我的代码:
$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);
下面是我张贴的形式。我没有在javascript中找到对form1的任何引用。
<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>
我想这些标题,以更好地模拟浏览器,但无济于事:
$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);
好,你得到的是相当翔实的错误:'错误:文件上传不处理。请确保选择指定模板格式的适用excel文件进行上传。所以我认为JSP正在对上传的文件执行一组测试。你确定你发送的Excel文件实际上符合他们希望符合的任何模板吗? – fvu
是的,我可以通过浏览器上传相同的文件,它可以工作。 – George
从html显示'