TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Upload a file using Request::factory
  • I know I can upload files using cURL directly, but is there any way to do so using Request::factory?

  • There is always a way :)

     $file = file_get_contents('/location/of/file');
    
     $request = Request::factory('http://someurl.tld/foo/bar')
            ->method(HTTP_Request::POST)
            ->body($file)
            ->headers(array(
                     'content-type' => 'application/x-set-correct-mime',
                     'content-length' => strlen($file)
            ));
    
     $request->execute();
    
  • Curious, if you had a form with file and input fields, how do you send both? I assume you can use post() in conjunction with body() @samsoir?

  • Exactly, I wanted to simulate a form post - fields & file upload(s). Is that possible too @samsoir?!

  • The native driver uses php stream functions so it should be possible but sending a multipart looks more difficult, http://www.php.net/manual/en/function.stream-context-create.php#90411.

  • In Kohana 3.1.3 Request::post() has precedence over Request::body(), so no it is not directly supported. However you can construct your own valid multi-part message within the Request::body() containing the file data and the post values if you like - that will be supported by all drivers.

    Curl supports multipart messaging out of the box, as does PECL HTTP, but the current Kohana interface does not. I have been toying with the idea of multipart messaging for Ko 3.2.

    So the short answer is, yes you can do it but you have to construct your own multipart message and apply it to the Request::body().

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion