0

I have the following HTML form:

<form enctype="multipart/form-data" id="upload_form" role="region" action="remoteUpload2.php?command=FileUpload&amp;type=Files&amp;currentFolder=%2F&amp;langCode=en&amp;hash=8e402b8b9927640d&amp;" method="POST" target="ckf_19">
<input name="upload" type="file">
<input value="Upload Selected File" type="submit">
<input name="cancel" value="Cancel" type="button">
</form>

When I submit it, if I do a print_r($_FILES) I get the following output:

Array
(
    [upload] => Array
        (
            [name] => logo.png
            [type] => image/png
            [tmp_name] => /tmp/phpvIFI0K
            [error] => 0
            [size] => 12201
        )

)

What I am trying to figure out is how to send a file the same way, using only PHP, to connect two different systems together. I can modify the sending script, but not the receiving script, so I need to send data in the format that is expected.

Is there a way using cURL for me to post data to this script that will result in similar output for $_FILES? I have the file I want to send on the server, I just need to figure out how to POST it to the receiving script.

Sherwin Flight
  • 2,345
  • 7
  • 34
  • 54
  • seems like you want to upload file using php's CURL .. check [this](http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) and [this](http://code.stephenmorley.org/php/sending-files-using-curl/) once. – Mittul At TechnoBrave Dec 28 '16 at 08:54

1 Answers1

0

I was able to solve this problem with the following code:

// initialise the curl request
$request = curl_init('http://www.example.com/connector.php');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('logo.png') . ';filename=logo-test.png'. ';type=image/png'
    ));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

if(curl_exec($request) === false)
{
    echo 'Curl error: ' . curl_error($ch)."<br>";
}
else
{
    echo 'Operation completed without any errors<br>';
}

// close the session
curl_close($request);
Sherwin Flight
  • 2,345
  • 7
  • 34
  • 54