tags:

views:

88

answers:

3

Hi. I'm trying to use cURL to forward on a http request from a form in a web application I'm developing. Essentially I need to submit the same form twice on different servers, for the second server I'm adding some security measures to the post data.

I can receive $_POST information on the second form perfectly fine, however I'm having major troubles with my $_FILES - I've tried separating the two so there's a separate request for post and files data but still no luck.

Is this possible?

+1  A: 
<?php
$filename = '/foo/bar';
$postargs = array(
   'foo' =>'bar', //normal postfield
   'fileentry' => '@'.$filename //be sure to give it as an absolute path!, $_FILES['fileentry']['tmp_name'] usually has this
 );

$ch = curl_init();
//other stuff with curl
curl_setopt($ch,CURL_POSTFIELDS, $postargs);//give as array for proper encoding.
curl_exec();
?>
Wrikken
No it wouldn't, otherwise it would be a very strange answer to the question, wouldn't it? The manual is your friend: http://www.php.net/curl_setopt
Wrikken
@Wrikken sorry, I checked the manual and deleted the comment before your answer. I now see it doesn't. Still, strange syntax. I wonder if I wanted my field value to start with `@`...
Artefacto
Always wondered that myself. I suspect the php curl implementation is somewhat of the olden days, the time they thought magic_quotes was a good idea etc. :). And as soon it's in, you can't take it out without breaking code.
Wrikken
I tried similar to this with no joy
Toby
It works perfectly here, so I'm very curious what the actual headers are you receive on the server?
Wrikken
A: 

You can:

  • force PHP not to process the files (see this question for details);
  • use curl to submit file_get_contents('php://input') as raw post data (see this question for details). You will also have to forward some HTTP headers with CURLOPT_HTTPHEADER, namely Content-Length and Content-type.

This avoids hitting the filesystem.

Artefacto
php://input is not populated on a enctype multipart/form-data request as far as I know, and that's a requirement for fileuploads, so you cannot send the file that way. See: http://nl2.php.net/manual/en/wrappers.php.php
Wrikken
@Wrikken: hence the first part "force PHP not to process the files"
Artefacto
@Wrikken PHP won't see the request as being `multipart/form-data` if you follow the answer in the linked question.
Artefacto
Ah, my bad, I do apologize. As long as the OP doesn't need the file locally or is willing to process it 'by hand' a valid solution indeed. A bit hackish, but ultimately workable.
Wrikken
A: 

Sorry - I'm an insane noob, I can't reply to threads because I didn't register. But @Wrikken just wanted to say your solution did work after all, what I had tried previously was similar but different. Cheers for your help

Toby