Flex Mobile: Upload Photo to Server with PHP

The code below uploads a photo from a Flex mobile application to a server with PHP. When I was first searching on how to do this I came across a good tutorial on Hybrid Hacking. So I’ve basically just modified their code to work on mobile with the CameraUI and CameraRoll classes. Download the Flex project here.

The Flex mobile application has one view which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
		xmlns:s="library://ns.adobe.com/flex/spark"
		title="Photo Upload">
	
	<fx:Script>
		<![CDATA[
			private var urlRequest:URLRequest = new URLRequest("http://www._____.com/upload.php");
			private var file:File;
			
			//take a new picture with the camera
			protected function uploadCamera_clickHandler(event:MouseEvent):void
			{
				if (CameraUI.isSupported)
				{
					trace("camera is supported");
					var myCam:CameraUI = new CameraUI();
					myCam.launch(MediaType.IMAGE);
					myCam.addEventListener(MediaEvent.COMPLETE,selectCompleteHandler);
				}
				else
				{
					trace("camera not supported");
					statusText.text = "Camera not supported on this device.";
				}
			}
			
			//select a picture from the camera roll (gallery)
			protected function uploadGallery_clickHandler(event:MouseEvent):void
			{
				if (CameraRoll.supportsBrowseForImage) 
				{
					trace("camera roll is supported");
					var roll:CameraRoll = new CameraRoll();
					roll.browseForImage();
					roll.addEventListener(MediaEvent.SELECT,selectCompleteHandler);
				}
				else
				{
					trace("camera roll not supported");
					statusText.text = "Camera roll not supported on this device.";
				}
			}
			
			//when the selection is complete upload it
			protected function selectCompleteHandler(event:MediaEvent):void
			{
				trace("event.data.file.url; = "+event.data.file.url);
				takePhotoButton.enabled = galleryPhotoButton.enabled = false;
				file = event.data.file;
				file.addEventListener(Event.COMPLETE,uploadCompleteHandler);
				file.addEventListener(Event.OPEN,openUploadHandler);
				file.upload(urlRequest);
				
			}
			
			protected function uploadCompleteHandler(event:Event):void
			{
				trace("upload complete");
				takePhotoButton.enabled = galleryPhotoButton.enabled = true;
				statusText.text = "Photo Uploaded";
			}
			
			protected function openUploadHandler(event:Event):void
			{
				trace("uploading");
				statusText.text = "Uploading...";
			}
		]]>
	</fx:Script>
	
	<s:VGroup x="21" y="23" width="200" height="200">
		<s:Label id="statusText" fontSize="24" text="Choose a photo..."/>
		<s:Button id="takePhotoButton" label="Take Photo" click="uploadCamera_clickHandler(event)"/>
		<s:Button id="galleryPhotoButton" label="Upload from Gallery"
				  click="uploadGallery_clickHandler(event)"/>
	</s:VGroup>
</s:View>

The PHP code (from Hybrid Hacking) looks like this:

<?php
$tempFile = $_FILES['Filedata']['tmp_name'];
$fileName = $_FILES['Filedata']['name'];
$fileSize = $_FILES['Filedata']['size'];
move_uploaded_file($tempFile, "./" . $fileName);
This entry was posted in Uncategorized. Bookmark the permalink.
  • http://www.coldfusionjedi.com Raymond Camden

    Just an FYI, I’m using this in my presentation today. I have it going to a CFM page that will create a thumbnail. I then use a web based front end ‘picture viewer’ (along with jQuery). I think it’s going to work out great. Flex to CF to jQuery – lots of buzz words at once. Anyway – I definitely credit you in the code (and will in the preso), but I wanted to say thank you!

    • Chris

      Is the code from this presentation available online.
      Thanks

      • Chris

        I used your example and posted code that will work on iOS:

        http://iappbuilder.net/index.php/ios-image-upload

        Thanks again for posting your example.

        • Vijayakumar Gopalakrishnan

          Great Work thanks
          best
          vj

        • http://www.bigcatdigital.com/ James

          The link doesn’t work??

  • Jason

    Matt great post. Have you tried this on iOS? When I did it just seems to hang on the file.upload. Neither the COMPLETE or OPEN events get fired.

    • Fabio Biondi

      same problem

    • Dandre Gregory

      Which version of iOS?

  • http://splarchive.wordpress.com Art Holland

    First, thanks for a very helpful bit of code. Unfortunately, I’m in the same boat as Jason. No problems on Android but the photo upload fails on iOS.

  • Jord

    Hello world

  • Filipe Duarte

    hi… good example but dont work…Can you help me?
    error #2044: Unhandled IOErrorEvent:. text=Error #2038: File I/O Error.
        at flash.media::MediaPromise/get file()
        at views::upload_imagemHomeView/selectCompleteHandler()

    • http://www.chaosm.net matt

      I had this same error, but with an app for the Playbook. Does your app have permission to access the files?

      • Filipe Duarte

        yes put all permission on android project!!! can you help me?

      • Filipe Duarte

        i didn’t change noting but now working… thanks

    • Ssh

       Does anyone know how to resolve the File I/O Error problem?

  • Filipe Duarte

    Its possible do video upload?

    • matt

      I haven’t tried it yet, but it should work for small videos. Depending on your server there could be timeout issues with PHP. You also need to allow the user to select video files.

    • http://www.chaosm.net matt

      I haven’t tried video yet, but it should work. You would need to allow users to pick video files. There could also be timeout issues with PHP.

  • duck

    Hi guys,
    Not work on IOS !
    Have you an idea ?
    Thanks

    • Ploloduasatu

      same problem here…

  • Chinthaka

    Thanks. Its working with PlayBook OS 2. 

  • Usmanbinafan

    nice code

  • B_jevan

    Hey, nice code!

    Do you have a
    example for how to do this same think with sound!

  • amy

    is it possible to change the size of the image captured?