我在通过cURL发送$_FILES时遇到了一些麻烦 – 使用以下代码将文件传输好了,但是我无法获取文件的名称和类型,实际上一旦$_FILES到达其目的地的类型存储为“application / octet-stream” – 我做错了什么!?
$count=count($_FILES['thefile']['tmp_name']);
for($i=0;$i<$count;$i++) {
if(!empty($_FILES['thefile']['name'][$i])) {
$postargs[$i] = '@'.$_FILES['thefile']['tmp_name'][$i];
}
}
$header = array("Content-type: multipart/form-data");
$ch = curl_init('http://localhost/curl/rec.PHP');
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch,CURLOPT_ENCODING,"");
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postargs);
curl_exec($ch);
curl_close($ch)
解决方法:
你应该像这样构建$postargs,
$postargs = array();
foreach ($_FILES as $param => $file) {
$postargs[$param] = '@' . $file['tmp_name'] . ';filename=' . $file['name'] . ';type=' . $file['type'];
}