views:

37

answers:

2

Hey,

I have an image slideshow program working right now and it takes in a folder of a hard coded in number of images. I would like to change this so that it can take in a folder and will display all of them no matter the number. Is there a way to do this in flash? I'm thinking something like the foreach loop in perl or other scripting language. It is possible to store then number of images in a text file but I also don't know how to read that in flash either. I'm working in actionscript 3. Any help would be greatly appreciated.

Thanks -Mike

+1  A: 

Hi there,

Try this:

var folder : File = new File('path');
folder.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListHandler);
folder.getDirectoryListingAsync();

--

private function dirListHandler(event : FileListEvent) : void
{
   for each(var file : File in event.files)
   {
      trace(file.url);
   }
}

You need to compile to and AIR application for this.

HTH

Zárate
This does not work for me. This is for an online web application and the folder is on my server
msandbot
Flash is a client-side technology, so if your images are on the server, you need something running server side. Either PHP, Ruby, Java, Perl... whatever.Just get one of those languages to dynamically write an XML and feed that to Flash.
Zárate
+1  A: 

@Zarate is correct that you need to use a server-side scripting language.

If you choose PHP, look into readdir, which "Returns the filename of the next file from the directory." [PHP Manual]

Here is a PHP class I created for retrieving the filenames of all files in a directory:

class DirectoryContentsHandler
{
    private $directory;
    private $directoryHandle;
    private $dirContents = array();

    public function __construct($directory)
    {
      $this->directory = $directory;
      $this->openDirectory();
      $this->placeDirFilenamesInArray();
    }

    public function openDirectory()
    {
      $this->directoryHandle = opendir($this->directory);
      if(!$this->directoryHandle)
      {
        throw new Exception('opendir() failed in class DirectoryContents at openDirectory().');
      }
    }

    public function placeDirFilenamesInArray()
    {
      while(false !== ($file = readdir($this->directoryHandle)))
      {
        if(($file != ".") && ($file != ".."))
        {
            $this->dirContents[] = $file;
        }
      }
    }

    public function getDirFilesAsArray()
    {
      return $this->dirContents;
    }

    public function __destruct()
    {
      closedir($this->directoryHandle);
    }
}

Here is how to use the class listed above:

$directoryName = 'some_directory/';
//Instantiate the object and pass the directory's name as an argument
$dirContentsHandler = new DirectoryContentsHandler($directoryName);
//Get the array from the object
$filesArray = $dirContentsHandler->getDirFilesAsArray();
//Display the contents of the array for this example:
var_dump($filesArray);

Beyond that, you could either echo the contents of the array and send them as a string of variables to the SWF, or (this is the better choice if there will by LOTS of images) use PHP to create an XML file that contains the filenames, then send that file to the SWF. From there, use Actionscript to parse the XML, load the image files, and display them on the client's side.

letseatfood