$_FILES 空,卷曲文件上传

Bra*_*tin 3 php wordpress curl

我一直在用头撞墙。我觉得我的问题真的很愚蠢,但我无法弄清楚是什么。

我有一个代理脚本,它处理一切(POST 数据和上传)。

当我在后端服务器上打印_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);
Run Code Online (Sandbox Code Playgroud)

我使用 http_build_query 而不是只提供一个数组的原因是因为我们会遇到一个多维数组。

谢谢!

小智 5

我愿意打赌你的表单 enctype 不是“multipart/form-data” b/c 你没有将 post 字符串作为数组传递。

“如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data。从 PHP 5.2.0 开始,如果文件以 @ 前缀传递给此选项,则 value 必须是一个数组。”

您可以通过设置 curl 选项“curl_setopt($ch, CURLOPT_VERBOSE, TRUE);”然后“print_r(curl_getinfo($ch));”来了解 curl 发生了什么。在“curl_exec($ch);”之后 它将输出有关请求和响应的所有信息。