[wp-hackers] WP_Http File Upload

Beau Lebens beau at dentedreality.com.au
Wed Nov 18 22:42:47 UTC 2009


> I'm working on a project that requires HTTP file uploads via POST. Does
> anyone (Jacob?) know how the request should be written using the WP_Http
> class? There doesn't seem to be any code that handles file uploads, but I'm
> hoping I don't have to write an extender class just for that use case.

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.

HTH
Beau


More information about the wp-hackers mailing list