Uploading a picture for User Profile Picture via Flash

Events happening in the community are now at Drupal community events on www.drupal.org.
alexandre_fs's picture

Hello,

I am trying to upload photos for the user profile picture from a Flash client via AMFPHP server.
The idea is to make it possible to upload a picture when a user is registering (via Flash).

I've been searching a lot of related posts and I've came up with this solution based on this post: http://groups.drupal.org/node/22407
I've made some changes and here's my code:

Client-side (Flash), called after selecting a file:

      private function fileLoaded(event:Event):void
     {
          trace("File Loaded. UID: " + uid); // uid is a global variable
           trace(imageFile.data); // this bytearray here appears to be OK... ( i can't make a breakpoint because I'm using a framework that does not allow me that, but by seeing the console, it seems ok...)
         

          var ba:ByteArray = imageFile.data; //event.target.data gets the same result...
         ba.compress();
         var myResponder:Responder = new Responder(onFileSave, onFaultUploadPhoto);
         myService.call('partytravellers.uploadPhoto', myResponder, ba, uid);
     }

Server-side:

// definition of method...
       array(
         '#method' => 'partytravellers.uploadPhoto',
            '#callback' => 'uploadPhoto',
           '#access callback'  => 'upyt_service_file_upload',
          '#args' => array(
             array(
                 '#name'         => 'data',
                  '#type'         => 'struct',
                    '#description'  => t('Save bytearray data to a file'),
                  ),
             array(
                 '#name'         => 'uid',
                   '#type'         => 'int',
                   '#description'  => t('User ID'),
                    ),
             ),
         '#return' => 'int',
         '#help' => 'Upload file and update user',
       ),


//callback
function uploadPhoto($fileData, $userId)
{
// sorry for all the flog_it, but they are useful to detect where it's failing...

flog_it($fileData);
flog_it($fileData->data);
flog_it($userId);


    $file = tempnam(realpath(file_directory_temp()), 'file');
flog_it($file);
    if (!$fp = fopen($file, 'wb'))
  {
      return -1;
}
flog_it('b');
  // Here is the magic, we just need to add gzuncompress to the bytearray data
   $data = gzuncompress($fileData->data);
flog_it('c');
  fwrite($fp, $data);
flog_it('d');
    fclose($fp);
flog_it('e');
flog_it($file);
   if (!file_move($file, '/runway2/sites/default/files/'.$file, 'FILE_EXISTS_RENAME'))
   {
      return -1;
}
flog_it('f');
  // add file to user picture
    // still not implemented

  //return $file;

   return 0;
}

This is what i get in my logs:

[2010-05-24 17:47:36] Array
(
    [objectEncoding] => 3
    [length] => 4251
    [endian] => bigEndian
    [position] => 4251
)

[2010-05-24 17:47:36]
[2010-05-24 17:47:36] 0
[2010-05-24 17:47:36] /tmp/filePjvWgT
[2010-05-24 17:47:36] b

Meaning, the code breaks here: $data = gzuncompress($fileData->data);

I tried with or without gzip compression. Without it, it breaks when trying to move the file from the temp directory.

Meaning, for some reason I think $fileData->data does not exist. But the info of the file (size, etc...) are correct.

Any suggestion on this, or alternative way to implement?

I also considered the following approach but I don't quite understand it and the first one seems more appropriate ( I could do everything in just 1 server call...)
http://groups.drupal.org/node/13781

Also, how do I know which file type it is? I want to give the proper extension to the file...

Thank you

Services

Group organizers

Group categories

Group notifications

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

Hot content this week