Topic: Listing Files in a Folder

Anyone know how to output a list of all files in a folder with ActionScript?  Preferably as an array.

Re: Listing Files in a Folder

You could write a simple PHP script that reads to and then feeds it to ActionScript.

Re: Listing Files in a Folder

Doh!!  With all of it's powers now, I forget that ActionScript is still a client side language.  It's going to have to be PHP on the server side then.  Thanks for the reminder.

Re: Listing Files in a Folder

Yeah, it would be that hard to write such a thing in PHP, and then you could output it as XML or as a delimited variable list.

Re: Listing Files in a Folder

Just to jump in and offer some code of mine
PHP:

<?php
header("Content-type:text/xml");
function dir2xml($path) {
$dirname = $path;
if(strlen($dirname) > 3) {
	if(strrpos($dirname, "\\")) {$dirname = substr($dirname, strrpos($dirname, "\\")+1); }
	if(strrpos($dirname, "/")) {$dirname = substr($dirname, strrpos($dirname, "/")+1); }
	if(strrpos($dirname, DIRECTORY_SEPARATOR)) {$dirname = substr($dirname, strrpos($dirname, DIRECTORY_SEPARATOR)+1); }
	$path = $path.DIRECTORY_SEPARATOR;
}
echo "<node label=\"$dirname\" branch=\"true\">\n";
if ($dir = opendir($path))
   {
       while (false !== ($file = readdir($dir)))
       {
           if ($file != '.' && $file != '..')
           {
			   if(!is_dir($path.$file)) {
            		echo "<node label=\"".$file."\" />" ."\n";
				} else {
					dir2xml($path.$file);
				}
      		}
       }
       closedir ($dir) ;
   }
   echo "</node>\n";
}
//Set path here
dir2xml("../");  
?>

Actionscript:

var myTreeDP:XML = new XML();
myTreeDP.ignoreWhite = true;
myTreeDP.load("http://localhost/php_sandbox/dir.php");
myTreeDP.onLoad = function() {
  myTree.dataProvider = this;
};
var treeListener:Object = new Object();
treeListener.change = function(evt:Object) {
  trace("selected node is: "+evt.target.selectedNode);
  trace("");
};
myTree.addEventListener("change", treeListener);

Dumps the directory specified in the bottom of the code to a tree in Flash MX 2004 Pro

Use or not, I don't mind smile

Last edited by ChristianGeek (2006-04-10 13:13:10)

Re: Listing Files in a Folder

Thanks for the excellent code.  It'll sure save me some time.