0

I'm coding a kind of php proxy, for internar services.

I'm doing a pretty simple use of curl like this:

$ch = curl_init( $url );
//set timeout to infinit 
set_time_limit(0);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);

if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST) );
}

curl_setopt($ch, CURLOPT_HTTPHEADER, getallheaders());

curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

//Request
list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 );
$status = curl_getinfo( $ch );  
curl_close( $ch );

I need to include also in that curl exec the incoming file. I don't know the filename, just the form input name.

Question

1) How could I include the content of $_FILES in that CURLOPT_POSTFIELDS ? 2) Do I need to specify if it's a binary file ?

Thanks

Martin Borthiry
  • 5,256
  • 10
  • 42
  • 59

1 Answers1

-1

Here's a simple example of how to send files via cURL. You don't need to mention that it's a binary file.

$tmp = $_FILES['image']['tmp_name'];
$filename = basename($_FILES['image']['name']);

$data = array(
    'uploaded_file' => $tmp.';filename='.$filename,
);

$ch = curl_init();   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// set other cURL options here.

curl_exec($ch);

Now, on the other side you can receive the details in $_FILES.

receiver.php (assuming this is what cURL calls to to).

var_dump($_FILES)
Akshay
  • 2,244
  • 3
  • 15
  • 34
  • Thank you for your response, but that is not working. I'm receiving the tmp file path. This is the raw request: Content-Disposition: form-data; name="updoaded_file" /tmp/phphga6Mb;filename=Jellyfish.jpg. $_FILES is empty – Martin Borthiry Nov 18 '15 at 15:10
  • I've found the solution here: http://stackoverflow.com/questions/27228367/php-send-local-file-by-curl?lq=1 – Martin Borthiry Nov 18 '15 at 15:24