Correct Format/Headers for Posting to the File Service?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
jfsullivan's picture

First off let me say I really like the 3.0 version of the services. But I've come to a stumbling block on posting files to the file service that none of the examples have so far been able to answer for me. I'm writing a basic PHP uploader that will then take the submitted image and post it to the file service. This simplified client is as follows:

<?php
$postData
= array(
   
'file' => array(
     
'file' => base64_encode(file_get_contents($_FILES["Image"]["tmp_name"])),
    
'filename' => $fileName,
     
'filesize' => filesize($_FILES["Image"]["tmp_name"]),
    
'timestamp' => time()
));
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$query = http_build_query($postData, '', '&');

$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $query);
curl_setopt($curl, CURLOPT_HEADER, true);            
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);            
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_VERBOSE, true);

$response = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);
print
"<em>STATUS:</em>\n";
var_dump($status);
print
"<br><br><em>RESPONSE:</em>\n";
var_dump($response);
?>

The response I get from the File Service (going to an endpoint of http://drupal/endpoint/file) is a 200 but no file gets created. Looking within the file_resource.inc and the submission fails within the function _file_resource_create() at the first

<?php
// If the file data is empty then bail
 
if (!isset($file->file)) {
    return
FALSE;
  }
?>

(about line 141 in the .inc)

Looking at the logs, this seems to be due to my above post query being incorrectly formed. When it comes in as the $file parameter to _file_resource_create() the variable is an array of length 1 with all the post data jammed into that value such as:

0 = file%5Bfile%5D%5B0%5D=FaAAgBAQAB...&file%5Bfilename%5D=filename.jpg&file%5Bfilesize%5D=51685&file%5Btimestamp%5D=1317999491

So anyone have any insight into this? I've tried all sorts of variations of building the original $postData, tweaking the cURL options... what am I missing?

Thanks!

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week