views:

24

answers:

1

I need to get a random filename from a directory in AS3, and open it. (This will be online, so no AIR)

I'm thinking of just using a text/xml file with all the filenames listed, and just traverse through it getting the filename. But is there a more "direct" way to just look at the directory so I don't have to edit the text/xml file all the time?

+1  A: 

If you have php (or equivalent) on your server, I would send this out to a php script.

Call your choseRandomFileFromFolder.php file, and have it simply return the path to the file it has selected at random. Then you don't have to edit the text/xml file.

A quick google found this handy function which does most of what you'll need. You might need to tweak it to filter types etc, but it shouldn't be difficult, even if you're new to php.

Instead of simply dying at the end, you'll want to pass back a urlEncoded named variable. Don't forget to urlescape your path!

http://www.jonasjohn.de/snippets/php/random-file.htm

Stray
Thanks. I'll try that. How do I call a PHP function via AS3? EDIT: So there's no way to process a directory in AS3 alone?
john2x
You don't call a PHP function, you load a PHP file that does the processing you need (and outputs it to the page in a Flash digestible format)
Tegeril
Is there a formal documentation on how to do that? Thanks.
john2x
There are loads of examples around - you'll need to use URLLoader, to load a URLRequest with the .url property set to your script path. You listen for the COMPLETE event from that loader, and then you can access the data that has been sent back.
Stray
And no, I don't think there's a way to access a directory in AS3 alone without the Air file classes (not available in the browser).
Stray