?
Solved

How to dynamically load images into a swf?

Posted on 2008-11-01
7
Medium Priority
?
1,982 Views
Last Modified: 2013-11-11
I'm currently taking a very long line of photos, grouping them as a symbol and crawling them across a header, which is working out rather well.  Now I'm having the need to load images dynamically from a directory so that as more are added, etc. the Flash animation pulls those into the animation (in this case an AS2 coded tween).

I'm supposing that some sort of an `ls` or `dir` command may be available (if the swf is on the same server) in order to get the list of filenames necessary, but I'd like to see the code delineated fully in order to know how to properly execute this.  Also as this the swf is not run server side, perhaps some other tricks have to be brought into action in order to get this to function properly...  For example, will PHP have to be brought to bare for this?  Please advise.
0
Comment
Question by:gpsocs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 14

Accepted Solution

by:
Designbyonyx earned 2000 total points
ID: 22862455
Hi gpsocs.  Tthe only way to load all the images in a directory into an swf at runtime is to use a server side script to return a list of the files in the directory you specified.  I have done this same thing for a client, except mine was database driven.  Most of my server-side experience is in PHP and mySql, and it is important to note that if you plan on associating Titles, Hyperlinks, Authors, Descriptions, etc...  you will need a database or XML file to hold this infomation.  However, if you just need to simply load the images, then your task is very simple.  If you want a head start, check out flash's LoadVars class.  It fires an onLoad event, so you can execute code once all the data has loaded.  I have also attached a PHP and AS2 snippet I use as my starting point for loading files from a directory.  It requires a variable "path", and that is it.

IMPORTANT:  You must validate the data of the "path" variable that php receives!  I cannot stress the importance of this enough.  If you do not validate this variable (meaning, make sure that it does not contain malicious data) then you are pretty much giving a hacker free reign over your server.  Validatioin is more of a case by case thing, and if you need some help, just google php data validation.  

Let me know if you need any further explanation of how this works.  This script assumes it is located in the same directory as the html file that holds your swf. Take care.

~Ryan
//  Flash Code
var myVars:LoadVars = new LoadVars();
var pathToImages:STring = "path/to/images/"; // Make sure it ends in a slash /
var phpFile:String = "nameOfPhpFile.php";
 
myVars.path = pathToImages; 
myVars.sendAndLoad(phpFile, myVars, "POST");
 
myVars.onLoad = function(success:Boolean):Void {
     if(success) {
          for(var i:Number=0; i<myVars.numFiles; i++){
               // create or attach image holder here
               // using createEmptyMovieClip, attachMovie, or Loader class
               // Use  (pathToImage + myVars["file"+ i]) to load images
          }
     } else {
          // Error loading php file
     }
}
 
//  PHP Code
<?php
    
$files = array();
if(!isset($path)) $path = $_POST['path'];
 
$dir = opendir($path);   
while(($file = readdir($dir)) !== false) {   
	if($file !== '.' && $file !== '..' && $file !== '.DS_Store' && $file !== '_notes' && !is_dir($path.$file)) {
		$files[] = $file;
	}   
}
 
closedir($dir); 
  
sort($files);
 
$numFiles = count($files);
 
echo "numFiles=$numFiles&path=".urlencode($path)."&";
for($i=0; $i<$numFiles; $i++) {   
	$it = urlencode(htmlentities($files[$i]));
	echo "file$i=$it&"; 
}
 
?>

Open in new window

0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 22862460
Type on line 3, the capital "T" needs to be lower cased.  Sorry I typed that by hand and didn't catch it until after I posted.
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 23305535
Umm, I feel like I provided an adequate solution to this issue.  I feel like it could be a beneficial part of the knowledge base.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:gpsocs
ID: 31512312
Thanks for the previous information.  I have had a very hectic life and frankly had to step away from this project for the last two months and am now just getting back to it today.  Thanks for the information and I apologize for not accepting it sooner.
0
 

Author Comment

by:gpsocs
ID: 24189955
LoadVars is deprecated. Is there anything else I can use?
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 24191039
google.com ->  "loadvars as3"  ->  I'm feeling lucky
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question