我一直在为这个问题苦苦挣扎。我感觉我的问题可能很傻,但我就是想不出来。
我有一个代理脚本,它处理所有事情(POST数据和上传)。
当我在后端服务器上打印 print_r($_FILES) 时,它是一个空数组,而 $_POST 包含文件路径。
$ch = curl_init();
$options = array(
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array(
"Host: {$host}",
"Cache-Control: no-cache"
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_URL => "http://{$this->config->ipAddress}{$url}",
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_COOKIE => $cookie_string,
CURLOPT_USERAGENT => $_SERVER["HTTP_USER_AGENT"]
);
if (! empty($_POST)) {
if (! empty($_FILES)) {
//$options[CURLOPT_HTTPHEADER][] = "Content-type: multipart/form-data";
$files = "";
foreach ($_FILES as $fid => $file) {
$files .= "{$fid}=@" . $file["tmp_name"] . ";type={$file["type"]};name={$file["name"]}&";
}
}
$postString = (! empty($files) ? $files : '') . http_build_query($_POST);
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $postString;
}
curl_setopt_array($ch, $options);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
我使用http_build_query而不是直接传递一个数组的原因是因为我们会遇到多维数组。
谢谢!