php在使用CURL上传文件时出现413 Request Entity Too Large,网上也查找了很多方案,但是都不起作用
经仔细检查,发现curl_setopt的各个参数设置的顺序也会有影响。
<pre name="code" class="html"><pre name="code" class="html">$downloadPath = 'E:\wamp\www\11.mp3';
$uploadUrl = "http://localhost:8000/wx_index/55.php";
if (PHP_VERSION >= '5.5.0') {
$uploadData = array('file' => new CURLFile($downloadPath));
}
else {
$uploadData = array('file' => '@E:\wamp\www\11.mp3');
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $uploadUrl);
curl_setopt($curl, CURLOPT_POSTFIELDS, $uploadData);
curl_setopt($curl, CURLOPT_POST, true);
$voice = curl_exec($curl);
curl_close($curl);
上面代码执行会出现413错误。
将CURLOPT_POST和CURLOPT_POSTFIELDS位置对调,就能发送成功。代码如下;
$downloadPath = 'E:\wamp\www\11.mp3';
$uploadUrl = "http://localhost:8000/wx_index/55.php";
if (PHP_VERSION >= '5.5.0') {
$uploadData = array('file' => new CURLFile($downloadPath));
}
else {
$uploadData = array('file' => '@E:\wamp\www\11.mp3');
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $uploadUrl);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $uploadData);
$voice = curl_exec($curl);
curl_close($curl);
下面是55.php
print_r($_FILES);
copy($_FILES['file']['tmp_name'], $_FILES['file']['name']);
这只是本人遇到的问题,可能还有其他造成该错误的原因,具体要自己多实践了。对于其他的参数设置顺序是否会导致其他错误,还未经实践。如果发现,请给我留个言,以免以后走弯路。
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $uploadData);