views:

867

answers:

2

In Flex 3/AS 3, what would be a good way to check if a remote file exists? I'm thinking in PHP ways where you'd try to "fopen" a remote path (like "http://example.com/somefile.exe"), and see if it works or not. I'm not asking to just download all of the file, I just want to know if the file is there (and accessible).

A: 

You would probably need to attempt to load the file. If you get an IOError, the file doesn't exist (or your path is wrong). If it starts loading, by triggering a progress event then it exists. You can then cancel the remainder of the loading.

Alternatively you could try calling a PHP script from Flash which does what you have described, this could return a simple true/false.

Groady
The thing is, if the file does not exist, the web server returns a 404 page or similar (actually in my case, the front page is returned... blasted URL rewrites etc), and technically that page will be successfully downloaded.I'm going for a solution that is close to your alternate - I'm checking on the server side (using PHP in this case) if the file exists or not - and just adds a "no file found" parameter to the flashvars. Which means the checking doesn't really take place in the flash client application. I guess this is good enough for me now.
Jonny
A: 

There is a utility class I developed that handle a check weather a file exists or not. Here's the code: http://code.google.com/p/eladlib/source/browse/trunk/EladLibFlex/src/com/elad/framework/utils/FileExistsUtil.as

And implementation looks like this:

var fileExists:FileExistsUtil = new FileExistsUtil();
fileExists.checkFile("file.jpg", 
function(eventType:String):void
{
 trace(eventType);
}, 
function(errorType:String, text:String):void
{
 trace(errorType+": "+text);
});
Elad Elrom