[wp-hackers] WP_Http File Upload

Aaron Brazell emmensetech at gmail.com
Wed Nov 18 23:16:34 UTC 2009

On Wed, Nov 18, 2009 at 5:42 PM, Beau Lebens <beau at dentedreality.com.au>wrote:

> I believe you'd be able to do this via something like
> base64_encode()ing the contents of the file, then sending the other
> details along with the POST request just by setting that into the body
> of the request, as a query-string formatting string.
> You're likely to have some "fun" dealing with big files using this
> method though, don't know if that's going to be an issue for you.
> Something like this should be a starting point (not tested at all :) ).
> <?php
> $file = file_get_contents('filename.ext');
> $file = base64_encode($file);
> $body = http_build_query( array( 'file' => $file, 'name' => $name,
> 'whateverelse' => 'something' ) );
> $result = wp_remote_post( $url, array( 'body'=>$body );
> echo wp_remote_retrieve_body($result);
> ?>
> This is all assuming you're sending a file from WP to a remote site,
> and obviously the format will depend on what they're expecting.

Thanks Beau-

Your assumptions are incorrect about source and destination, but the
principle of your code seems valid. I'll give it a whirl tomorrow. I'm
rebuilding my PHP Twitter library based on BackPress (thus the WP_Http
class) since cURL can be so... bad. Changing avatars and background images
require a POST of the image files. We'll see how far I get. :-)

Thanks again,


More information about the wp-hackers mailing list