Page 1 of 1

Random image display in Flash

Posted: Mon Feb 09, 2009 4:12 pm
by icsworldadmin
Hi,

I have a site with a flash header that randomly pulls an image from a directory for the next image in the slideshow.

Right now it works, but only if the page is in the same directory as the folder for the pics. If the page is in a different directory, it will not load the images. Also, if the page is https instead of http it will not insert the images, and if the page is http://website.com instead of http://www.website.com it will not work.

here is the code that i think needs to be altered to allow for absolute path rather than relative?

Code: Select all

<?php
 
$dir = "pics";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
   $newPics[] = $filename;
}
 
sort($newPics);
 
for ($i = 1; $i < sizeof($newPics); ++$i) {
 
    $paths = $paths . $newPics[$i];
    if ($i < sizeof($newPics)-1){
        $paths = $paths . ',';
    }
}
 
echo "paths=" . $paths;
 
?>
any help would be appreciated. thanks

Re: Random image display in Flash

Posted: Tue Feb 10, 2009 8:19 am
by mbdigital
What does the echo in your php return?
I would also suggest using array_push($newPics,$filename) rather than $newPics[] = $filename;.
I would suggest something closer to the following - how does this go?
<?php

$dir = "pics";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh)))
{
array_push($newPics,$filename);
}

sort($newPics);
$paths="";
foreach($newPics as $file)
{
$paths = $paths . $file .",";
}
//remove the last comma
$paths = substr($paths, 0, -1);
}

echo "paths=" . $paths;

?>

Re: Random image display in Flash

Posted: Tue Feb 10, 2009 11:05 am
by icsworldadmin
url to the new random image to display in the slideshow

Re: Random image display in Flash

Posted: Tue Feb 10, 2009 2:55 pm
by mbdigital
Does it echo a path when the folder is in different place? Or is the problem on the flash side?

Re: Random image display in Flash

Posted: Tue Feb 10, 2009 3:22 pm
by icsworldadmin
not really sure to tell ya the truth. i ended up working around it by simply putting a copy of the pics folder in each directory. not the way i wanted to go, but it works. otherwise i couldn't get it to display for everything.

btw i tried the code you supplied, didnt work... course im sure you dont have enough info about what its doing to properly write something up.

thanks for the help, but unless you have a suggestion i think ill just leave well enough alone.

edit:

actually i am still having a problem, once it has gone thru and displayed each image 1 time it starts in on a loop of just 2 images, and keeps running them back to back indefinitely. any thoughts on that? better way to write up the random file code?

http://www.icsworld.com

Re: Random image display in Flash

Posted: Tue Feb 10, 2009 4:08 pm
by mbdigital
I would suggest outputting all the image paths in an xml format using the php. Import the xml to the flash and use actionscript' Math.rand() function to select an image from the xml object at random.
Accessing the folder should be no problem in the php as long as you can use url paths to open folders and files, I'd suggest checking that in the php settings. Firstly make sure the php is creating a good list of files before handling it in the actionscript.

There's a good tutorial at gotoandlearn.com for importing files to flash from xml.