1

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?

Toby
  • 11
  • 1
  • 2
  • You could try base64 encoding the image data and sending it as a post request, but I'm not sure it would work exactly the way your require. – Sam Becker Jun 08 '10 at 01:02

2 Answers2

2
<?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
  • 69,272
  • 8
  • 97
  • 136
  • 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 Jun 08 '10 at 01:16
  • 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 Jun 08 '10 at 01:18
  • 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 Jun 08 '10 at 01:21
  • It works perfectly here, so I'm very curious what the actual headers are you receive on the server? – Wrikken Jun 08 '10 at 02:05
0

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.

Community
  • 1
  • 1
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • 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 Jun 08 '10 at 01:18
  • @Wrikken: hence the first part "force PHP not to process the files" – Artefacto Jun 08 '10 at 01:19
  • @Wrikken PHP won't see the request as being `multipart/form-data` if you follow the answer in the linked question. – Artefacto Jun 08 '10 at 01:27
  • 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 Jun 08 '10 at 01:34