Search code examples
phpfile-uploadjquerycurlmultipart

Sending raw multipart data through jquery $.post() and php


i need to send raw multipart data with a php POST but without an html form... im starting the process with jquery $.post() instead (the objective is to change a twitter account's background).

How can i achieve that? This is my current (and still incomplete) code:

1) Image filename is inserted in this hidden input field:

<input type="hidden" id="profile_background_image_url" value="oats.jpg" />

2) when clicking on the submit button, a javascript function is triggered... and it calls:

$.post('helper.php',{
 profile_background_image_url:$('#profile_background_image_url').val()
});

3) helper.php has

$param = array();
$param['image'] = '/www/uploads/'.$_POST['profile_use_background_image'];
$status = $connection->post('account/update_profile_background_image',$param);

Notes:

  1. all the background files are inside the /www/uploads local directory.
  2. im using Abraham Williams' twitteroauth library 0.2

Bottom line, in step three i need to send $param['image'] in raw multipart data to the $connection object (twitter library).

Any ideas?

Some references: http://dev.twitter.com/doc/post/account/update_profile_background_image


Solution

  • Yeah i see now that hes building the post fields array into a query string which means you have to manually set the content type and that the @ key in the image fields wont do its magic since that only works with an array argument. More importantly i dont see a way to modify the headers without hacking the library or extending it and replacing certain functions.


    I would try would be prepending @ to the file path of the image param like:

    $param['image'] = '@/www/uploads/'.$_POST['profile_use_background_image'];
    

    That is the convenient way to do it with cURL, and it looks like the libray basically uses cURL to make the request, so that should work.