Help tweeting images from mysql db


I am attempting to use the code below to post a status and an image to twitter. The library is tmhOAuth, the image is coming from a blob in my mysql db as is the mime type and file name.

Basically if an image is found stored in the db row then it sends to twitter using a post to “update_with_media” if no image is found then it just sends the status update alone using “update”. the “update” works fine, but I can’t get the update with media working. I’ve tried several variations from various sites including Twitter’s dev site but no joy. I’d really appreciate another set of eyes oh this.

if(!empty($imgid)) {

$getimg = mysql_query("SELECT filedata, filename, mime_type FROM post_images WHERE id = $imgid");
$img = @mysql_fetch_array($getimg); 

$imagedata = $img['filedata'];
$imagename = $img['filename'];
$imagetype = $img['mime_type'];                                     

$theimg = "@{$imagedata};type=$imagetype;filename={$imagename}";
$hasimg = "y";                  

if($hasimg == "y") {
    $code = $connection->request( 'POST',  
        'media[]'  => $theimg,
        'status'   => $thetweet),
    true, // use auth
    true  // multipart
} else {
    $code = $connection->request('POST', 
    array('status' => $thetweet));

// A response code of 200 is a success
if ($code == 200) {
  print "Tweet sent";
} else {
  print "Error: $code";


…figured it out - in case anyone else has a similar issue my problem was formatting. Instead of using the variable “$theimg” in line 10, I replaced line 18 with: ‘media[]’ => “$imagedata; type=$imagetype; filename=$imagename”

Hope that helps someone…