0

I'm currently building an upload proxy script that will upload a user uploaded file to other server. So I searched for building a multipart form data through PHP and built this stuff.

<?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;
}

?>

And for the form for submitting the form to my script:

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

But whenever I try to upload files, the $_POST and $_FILES superglobals are empty.

array(0) { }

Any suggestions? Thanks.

Raphael Marco
  • 494
  • 10
  • 21
  • 1
    First of all, check if the file you want to post isn't bigger than the server's size limit. When a file is too big, `$_FILES` is empty. – Gil May 11 '14 at 07:33
  • The file size limit is 100M, I'm uploading a <100KB file. The $_POST also returns empty array, so there's really wrong with this multipart data that I can't figure out. – Raphael Marco May 11 '14 at 17:05
  • Then I have no idea. Take a look at the answer [here](http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php), maybe some of this will help. – Gil May 11 '14 at 17:32

0 Answers0