天天看點

PHP CURL上傳檔案出現413 Request Entity Too Large

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