我正在尝试使用 cURL 使用 PUT 上传文件,其内容类型为 application/octet-stream,但即使我使用 curl_setopt 设置它,内容类型始终为“text/html”。我已经在 firefox 和 chrome 中进行了测试,看看它是否是浏览器的怪癖或其他东西(不是),所以我不确定我能做些什么。
error_reporting(E_ALL);
ini_set('display_errors', 1);
$ch = curl_init();
$localfile = 'upload.txt';
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, $url.$localfile.'?type=file');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("content-type: application/octet-stream"));
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_setopt($ch, CURLOPT_USERPWD, $un.":".$pw);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$msg = curl_exec ($ch);
$error_no = curl_errno($ch);
$print = curl_getinfo($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
谢谢阅读。