带有cURL POST的PHP多部分表单导致空数组

时间:2014-05-11 06:06:20

标签: php curl multipart

我目前正在构建一个上传代理脚本,用于将用户上传的文件上传到其他服务器。所以我搜索了通过PHP构建多部分表单数据并构建了这些东西。

<?php

$payload = array(
    'key' => 'XXXXXXXXXXXXXXXXXXXXX',
    'time' => time()
);

$payload = http_build_query($payload);

if((!empty($_FILES['image']))){
    $params = "------UploaderFormBoundary\r\n" .
              "Content-Type: application/x-www-form-urlencoded\r\n" .
              "\r\n" .
              $payload . "\r\n" .
              "------UploaderFormBoundary\r\n" .
              "Content-Type: " . $_FILES['image']['type'] . "\r\n" .
              "Content-Disposition: attachment; filename=\"" . $_FILES['image']['name'] . "\"\r\n" .
              "\r\n" .
              file_get_contents($_FILES['image']['tmp_name']) . "\r\n" .
              "------UploaderFormBoundary--";

    $first_newline = strpos($params, "\r\n");
    $multipart_boundary = substr($params, 2, $first_newline - 2);

    $request_headers = array();
    $request_headers[] = 'Content-Length: ' . strlen($params);
    $request_headers[] = 'Content-Type: multipart/form-data; boundary=' . $multipart_boundary;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, file_get_contents('http://xxxxxxxxxxxxxxxxxxxxxxx'));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;
    exit;
}

?>

对于将表单提交到我的脚本的表单:

<form action="gae_uploader.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image" />
    <input type="submit" />
</form>

但每当我尝试上传文件时,$ _POST和$ _FILES超全局都是空的。

array(0) { }

有什么建议吗?感谢。

0 个答案:

没有答案