Home » ActionScript 3.0, Adobe, Flex 3, PHP

Folders Listing in Flex 3 and PHP

3 September 2009 No Comment

In this sample we’ll use the Flex HTTPService class to make a PHP call and get the file list from a webserver folder. Then we’ll display them populating a List component and we’ll enable the mouse click on each item to save them on your disk (using FileReference.download() method)

fx3folderlisting

Try the online demo

FLEX CODE

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
 
	<mx:Script>
		<![CDATA[
			import flash.net.navigateToURL;
			import mx.events.ListEvent;
			import mx.collections.ArrayCollection;
			import mx.rpc.events.ResultEvent;
 
			private var fr:FileReference
 
			// Debug Path
			//private const PHP_PATH:String =  "http://localhost/_Formazione/ArticoliOnLine/Flex/2009_09_03_DirectoryListInPhp/src/";
 
			// Final Path
			private const PHP_PATH:String =  "";
 
 
 
			/**
			 * File List update (make PHP call)
			 */
			private function updateList():void {
 
 
				// DEBUG inside Flex
				phpRPC.url= PHP_PATH + "php/filesystem.php?action=list_files";
 
				// FINAL version
				//phpRPC.url="php/filesystem.php?action=list_files";
				phpRPC.send()
 
			}
 
 
 
			/**
			 * Called when PHP call is completed 
			 */
			private function resultHandler(e:ResultEvent):void {
 
				// We split the result string with the file list creating 
				// an array useful to populate the List Data Provider 
				fileList.dataProvider = new ArrayCollection( phpRPC.lastResult.file_names.split("|") )
 
			}
 
 
 
 
			/**
			 * Invoked after every List Selection 
			 */			
			private function listClick(e:ListEvent):void {
 
				// To Open the selected File in a new Window decomment following row
				// navigateToURL(new URLRequest("php/uploads/" + e.currentTarget.selectedItem))
 
				// To Open the Browser Window
				fr = new FileReference();
				var request:URLRequest = new URLRequest();
				request.url = PHP_PATH + "uploads/" + e.currentTarget.selectedItem;
        		fr.download(request);
			}
 
		]]>
	</mx:Script>
 
 
	<mx:HTTPService 
		id="phpRPC" 		
		resultFormat="flashvars"
		result="resultHandler(event)"
		/>
 
 
	<mx:Button label="Show files" click="updateList()"/>
	<mx:List id="fileList" change="listClick(event)"></mx:List>
 
</mx:Application>

THE PHP CODE:

filesystem.php (called from the Flex file)

<?php
 
 
	require_once "class_filesystem.php";
 
	// CARTELLA  SU CUI EFFETTUARE LE OPERAZIONI
	$base_dir = "../uploads/";
 
	$fs = new CFileSystem();
 
 
	switch($_GET["action"])
	{
 
 
		// Determinazione dei file contenuti all'interno di una cartella
		case "list_files":
 
			$files = $fs->ListFiles($base_dir . $_GET["folder"]);
 
			if (!$files)
				$result = false;
			else
			{
				// Preparazione output per Flash
				echo "file_names=" ;
				foreach( $files as $file )
				{
					// Inserire nell'IF altri file che non devono essere presi in considerazione
					if ( $file != "." &&
						  $file != ".." &&
						  $file != ".htaccess" &&
						  $file != "index.php" &&
						  $file != "cgi-bin" )
					{ 
						echo urlencode($file) . "|" ;
					};
				};
				echo "&" ;
 
				$result = true;
			}
			break;
 
 
 
 
	}
 
 
 
	// Output per Flash - Esito dell'operazione
	if ($result)
		echo "result=1&";
	else
		echo "result=0&";
 
 
 
?>

class_filesystem.php(called from the previous PHP file)

<?php
 
 
	class CFileSystem
	{
 
 
 
		function ListFiles($folder_path)
		{
			$files = array();
 
			$d = @dir($folder_path);
 
			if ($d == "")
				return false;
 
			while($entry = $d->read())
				array_push($files,$entry);
			$d->close();
 
			return($files);
		}
 
 
 
	}
 
?>

Download Source Code (FX3-PHP)

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.