View Full Version : Php to flash

02-02-2009, 01:41 PM
I hope someone can help.
I need some help linking flash and php.
I have the following actionscript code that limits file types to a given set. I can comment out those I don't want. However I would like to be able to set the file types dynamically from a PHP file and I have no idea how to do that.

var allTypes:Array = new Array();

var imageTypes:Object = new Object();
imageTypes.description = "Authorised files (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;*.tiff;*.tif;*.zip;*.rar;*.doc;*.pdf;*.xls;*.sit;*.ppt;*.csv;*.txt;* .rtf;)";
imageTypes.extension = "*.jpg; *.jpeg; *.jpe; *.gif; *.png; *.tif; *.tiff; *.zip; *.rar; *.doc; *.pdf; *.xls; *.ace; *.sit; *.ppt; *.csv; *.txt;*.rtf;";
var imageTypes:Object = new Object();
imageTypes.description = "Images (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;*.tiff;*.tif;)";
imageTypes.extension = "*.jpg; *.jpeg; *.jpe; *.gif; *.png;*.tiff;*.tif;";

var imageTypes:Object = new Object();
imageTypes.description = "Archives (*.zip;*.rar;*.ace;*.sit;)";
imageTypes.extension = "*.zip; *.rar; *.ace; *.sit;";

var textTypes:Object = new Object();
textTypes.description = "Text Files (*.TXT;*.RTF;)";
textTypes.extension = "*.txt; *.rtf;";

var FLRef:FileReferenceList = new FileReferenceList();

I am assuming I should import a variable i.e.

var textTypes:Object = new Object();
textTypes.description = VariableDesc;
textTypes.extension = VariableExt;

where (from the php file)
$VariableDesc = "Text Files (*.TXT;*.RTF; )";
$VariableExt = "*.txt; *.rtf";

Any suggestions, thanks?

02-02-2009, 03:35 PM
You could do one of two things. First is to have PHP output the list of file extensions as a string for Flash, and then have Flash split the string based on something like the | sign.

Or you can implement AMPHP http://www.amfphp.org/ which is a much more expansive solution. If you plan on doing more with Flash and PHP, this is a good suggestion.

If you are just trying to do this one thing, I would just use loadVars() and load a PHP file which will export the list as a string and split the string.

02-03-2009, 01:40 PM
Thanks for the suggestion.
Managed to find an example to modify and it seems to work

var allTypes:Array = new Array(); // create an array of file types

var imageTypes:Object = new Object();
var files_types:LoadVars = new LoadVars();

files_types.onLoad = function (success:Boolean):Void {
if (success) {
imageTypes.description = files_types.filetypes;
imageTypes.extension = files_types.fileextension;
} else {

imageTypes.description = "Authorised files (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;*.tiff;*.tif;*.zip;*.rar;*.doc;*.pdf;*.xls;*.sit;*.ppt;*.csv;*.txt;* .rtf;)";
imageTypes.extension = "*.jpg; *.jpeg; *.jpe; *.gif; *.png; *.tif; *.tiff; *.zip; *.rar; *.doc; *.pdf; *.xls; *.ace; *.sit; *.ppt; *.csv; *.txt;*.rtf;";

files_types.load( _root.urlUpload="../file_types.txt"); // Get the file

/* contents of params.txt:
&filetypes=Archives (*.zip;*.rar;*.ace;*.sit; *.gif;)&fileextension=*.zip; *.rar; *.ace; *.sit; *.gif;

allTypes.push(imageTypes); // add image types to array

Not sure it is as neat or correct as it could be as this is my first taste of actionscript.

02-03-2009, 02:19 PM
Looks good!

Here you can change the file from file_types.txt to file_types.php, so long as the PHP file outputs the same kind of string.

files_types.load( _root.urlUpload="../file_types.php"); // Get the file

02-03-2009, 03:06 PM
Thanks, quite pleased with myself on that.

If I change the extension from .txt to .php does that mean I can put in some php code to select data dynamically?
if ($variable == 'text') {
filetypes=Text Files (*.TXT;*.RTF; )
fileextension=*.txt; *.rtf
elseif ($variable == 'archive') {
filetypes=Archives (*.zip;*.rar;*.ace;*.sit; *.gif; )
fileextension=*.zip; *.rar; *.ace; *.sit; *.gif;
and so on....

02-03-2009, 04:58 PM
Correct, as long as your PHP outputs only the string in this format "&variable=stringstringstring", you can do any kind of preprocessing you wish in the PHP. Flash only will see the string that you echo at the end.

02-04-2009, 09:59 AM
I get it, only the following code does not work. It's fine when viewing the php page in webpage but flash only sees the last statement (the else bit).

$variable = "text"; // set a testing value for the variable

if ($variable == 'text') {
$filetype = '&filetypes=Text Files (*.TXT;*.RTF; )';
$fileext = '&fileextension=*.txt; *.rtf';

elseif ($variable == 'archive') {
$filetype = '&filetypes=Archives (*.zip;*.rar;*.ace;*.sit; *.gif; )';
$fileext = '&fileextension=*.zip; *.rar; *.ace; *.sit; *.gif;';
elseif ($variable == 'images') {
$filetype = '&filetypes=Images (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;*.tiff;*.tif; )';
$fileext = '&fileextension=*.jpg; *.jpeg; *.jpe; *.gif; *.png;*.tiff;*.tif;';
$filetype = '&filetypes=Authorised files (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;*.tiff;*.tif;*.zip;*.rar;*.doc;*.pdf;*.xls;*.sit;*.ppt;*.csv;*.txt;* .rtf;*.xdoc; )';
$fileext = '&fileextension=*.jpg; *.jpeg; *.jpe; *.gif; *.png; *.tif; *.tiff; *.zip; *.rar; *.doc; *.pdf; *.xls; *.ace; *.sit; *.ppt; *.csv; *.txt;*.rtf;';

echo $filetype.$fileext;


02-04-2009, 10:09 AM
You mean PHP outputs

&filetypes=Text Files (*.TXT;*.RTF; )&fileextension=*.txt; *.rtf

but Flash outputs

&filetypes=Authorised files (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;*.tiff;*.tif;*.zip;*.rar;*.doc;*.pdf;*.xls;*.sit;*.ppt;*.csv;*.txt;* .rtf;*.xdoc; )&fileextension=*.jpg; *.jpeg; *.jpe; *.gif; *.png; *.tif; *.tiff; *.zip; *.rar; *.doc; *.pdf; *.xls; *.ace; *.sit; *.ppt; *.csv; *.txt;*.rtf;

Also I think you might want to consider using a different deliminator than ; I like the | myself. That might help here and do you really need an * in front of them all?

I would think that this type of a string would be easier to process later in Flash. You could always pop the first entry as the title and then each of the rest as file extensions. I can't seem to find it, but I know there are a few characters that might cause some issues when used (of course & because it notes a new variable, so it must be urlencoded if you use it in your text).

&filetypes=Authorised files|.JPG|.JPEG|.JPE&fileextension=.jpg|.jpeg|.jpe|.gif

02-04-2009, 01:01 PM
Yes that's exactly the problem.

I have implimented your other suggestions, makes no difference though.

It is for a flash upload system but I want the type of files that can be uploaded to be limited dpending on where the user is coming from. I.e. if uploading to a gallery only images, uploading documents only textfiles/pdf etc. I would like to make it easy for the site admin to change the allowed extensions if need be, which is why I thought of an external file.

I could simply use seperate upload systems for each part of the site I suppose but that seems wasteful.

Maybe an easier solution would be to call a different external file i.e for the gallery call galleryfiles.txt, for documents call documentfiles.txt though again that seems more complicated.

02-04-2009, 03:33 PM
Can I see the Flash and the PHP page online somewhere? Or can you provide the source files to test?

02-04-2009, 04:18 PM
I have found the problem... and it is almost too embarassing to admit..but then this is my first attempt at flash stuff..
I think it was actually working all along.
The problem seems to be that when testing the flash movie (in flash) it was not parsing the php - which I get (finally), so I was checking it in the browser as well.
However I have just found that when testing the php page that includes the flash movie I need to close the page and reload it completely to test any changes, whereas I have simply been refreshing the page.
Of course it now makes sense- as the browser downloads the flash file into the temp folder and simply reuses it when I refresh the page rather than replacing it.

Anyway it seems to be working well.

All I now have to do is pass the correct variable to flash to tack onto the url of 'filestype.php' so it calls the correct set of extensions.

Thanks for your help so far..

02-04-2009, 05:00 PM
No problem, and hey I make similar mistakes all the time and I've been doing this for a while. Sometimes I think, why doesn't think work and I scratch my head and search an hours. Come to find out, since I was testing in Flash my variable from flashvars wasn't available...duh. It happens to everyone...somedays.