Skip to content

Instantly share code, notes, and snippets.

@okeez
Created October 17, 2013 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okeez/7018986 to your computer and use it in GitHub Desktop.
Save okeez/7018986 to your computer and use it in GitHub Desktop.
PlayerIO FTP Proxy for GameFS CDN file uploads
<?php
/*
PlayerIO FTP Proxy for GameFS CDN File Uploads from AS3
Author: Bob de Wit
Usage: create an AS3 request for an image:
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import mx.graphics.codec.JPEGEncoder;
import mx.utils.Base64Decoder;
import mx.utils.Base64Encoder;
var rect:Rectangle = new Rectangle();
rect.x = 0;
rect.y = 0;
rect.width = image.bitmap.width;
rect.height = image.bitmap.height;
var b64:Base64Encoder = new Base64Encoder();
var byteArray:ByteArray = image.bitmap.bitmapData.getPixels(rect);
var jpegArray:ByteArray = new JPEGEncoder().encodeByteArray(byteArray, image.width, image.height);
jpegArray.position = 0;
b64.encodeBytes(jpegArray);
var vars:URLVariables = new URLVariables();
vars.cmd = "save";
vars.dir = "/root/dir";
vars.sub = "optional_subdir";
vars.fn = "filename.jpg";
vars.b64 = b64.toString();
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.OPEN, openHandler);
loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.addEventListener(Event.COMPLETE, completeHandler);
var request:URLRequest = new URLRequest(php_script_url);
request.data = vars;
request.method = URLRequestMethod.POST;
loader.load(request);
private function completeHandler(event:Event):void
{
var xml:XML = new XML(event.currentTarget.data as String);
if(xml.@error == "")
{
trace("Image upload completed");
}
else
{
trace("Image upload error" + xml.@error);
}
}
*/
//We will be returning XML to as3
header ("Content-Type:text/xml");
$xml = new SimpleXMLElement("<result />");
//check the cmd
if(isset($_REQUEST['cmd']))
{
$cmd = $_REQUEST['cmd'];
$xml->addAttribute('cmd', $cmd);
switch($cmd)
{
case 'save':
//check if mandatory variables for save are set
if(isset($_REQUEST['fn']) && isset($_REQUEST['b64']) && isset($_REQUEST['dir']))
{
//Get the file name to use
$fn = $_REQUEST['fn'];
//Get the base64 encoded file
$b64 = $_REQUEST['b64'];
//Get the directory path
$dir = $_REQUEST['dir'];
//add the file name to the result XML
$xml->addAttribute('fn', $fn);
//Open FTP connection and login
$conn_id = ftp_connect('ftp.playerio.com');
$login_result = ftp_login($conn_id, 'your_userid', 'your_ftp_password');
//Return error if FTP login failed
if ((!$conn_id) || (!$login_result))
{
$xml->addAttribute('error', 'FTP connection failed');
}
else
{
//go to destination directory
ftp_chdir($conn_id, $dir);
//check for optional subfolder to create
if(isset($_REQUEST['sub']))
{
$sub = $_REQUEST['sub'];
ftp_mkdir($conn_id, $sub);
ftp_chdir($conn_id, $sub);
}
//temporarily save the file on the PHP Server
$ifp = fopen( $fn, "wb" );
fwrite($ifp, base64_decode($b64));
fclose($ifp);
//now upload the file
$upload = ftp_put($conn_id, $fn, $fn, FTP_BINARY);
// check upload status, ser XML error message if failed
if (!$upload)
{
$xml->addAttribute('error', 'FTP Upload Failed');
}
else
{
$xml->addAttribute('error', '');
}
//delete the temporary file
unlink($fn);
}
ftp_close($conn_id);
}
else
{
$xml->addAttribute('error', 'Invalid request: filename, directory or base64 not specified');
}
break;
default:
$xml = new SimpleXMLElement("<result />");
$xml->addAttribute('error', 'Invalid request or command');
break;
}
}
else
{
$xml->addAttribute('error', 'Invalid request or command');
}
echo $xml->asXML();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment