Folders Listing in Flex 3 and PHP
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)
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); } } ?>












Leave your response!