Fileupload using Zend AMF, RemoteObject and Flash 10

19 04 2009

In one of my earlier posts i described a method for uploading files using the RemoteObject class found in the Flex SDK. For the “back end” i used the still very popular amfphp implementation of the AMF protocol.

Wade Arnold the maintainer of amfphp is also involved in another implementation of the AMF protocol which is shipped with the Zend Framework. After reading some blog posts about this “new” package called Zend_Amf, i decided to give it a try.

Download ZendAMFUpload.zip for full example code.

I started my quest with downloading of the latest Zend Framework library, which is now at version 1.7.8 and followed the instructions of the Quickstart guide, which can be found here.

I also found a great post from Piotr Walczyszyn about how to merge the Zend_Amf package within the Zend Framework MVC structure and i used this approach as a starting point for my own example, but made the following changes compared to the tutorial of Piotr:

1)
Because i don’t use a database within my example i removed all code related to the config, database and registry from my bootstrap.php file.

2)
Somehow the disableLayout call in the MessageBrokerController init function Piotr provided resulted in an error, so i removed this line.

3)
Instead of putting my data objects into a dto folder i used a folder named vo. It is the same, but i am used to call my remote data objects Value Objects.

4)
During my quest i stumbled to some strange behaviour when sending Typed Objects (FileVO) from my flex client to the service layer. According to the Zend_Amf documentation you could use $_explicitType within a ValueObject for mapping serverside PHP classes with AS3 classes, but this only worked for retrieving typed objects, not for sending typed objects. Finally i found these issues related to class mapping of typed objects: ZF-5755, ZF-5885 and ZF-6130, and decided to use the $server->setClassMap call in my MessagebrokerController.

Here is the code i ended up with:

MessagebrokerController.php

class MessagebrokerController extends Zend_Controller_Action {
// Initialise controller with no view renderer
public function init() {
$this->_helper->viewRenderer->setNoRender(true);
}
// amf action
public function amfAction() {
$server = new Zend_Amf_Server();
$server->addDirectory(APPLICATION_PATH . '/services/');
// Mapping of PHP FileVO class and AS3 FileVO class
$server->setClassMap("FileVO", "FileVO");
echo($server->handle());
}
}

FileVO.php


class FileVO {
public $filename;
public $filedata;
}

RemoteFile.php


class RemoteFile {
/**
* Upload files!
*
* @param $file as a FileVO Object
* @return string
**/
public function upload($file) {
$data = $file->filedata;
file_put_contents( APPLICATION_PATH . '/uploads/' . $file->filename, $data);
return 'File: ' . $file->filename .' Uploaded ';
}
}

I kept the flex code the same as in my earlier post except for the remote service endpoint url, which should be now something like

http://[some-host]/messagebroker/amf

Download ZendAMFUpload.zip for full example code.


Actions

Information

2 responses

25 07 2009
John

Great article!
I suggest you change the title, because you made the integration of Zend MVC and Zend AMF, I looked this example and is not easily found.

5 11 2009
martin

was having trouble with this – was not liking the FileVO at all.

changed the remote class to this and all worked fine

public function upload($filename, $filedata) {
file_put_contents( ‘uploads/’ . $filename, $filedata);
return ‘File: ‘ . $filename .’ Uploaded ‘;
}

Leave a comment