Download FLA | example

I. INTRODUCTION

Ok, the aim of this tutorial is to provide a simple way to add a background (and/or a screensaver) to your website with text field showing how many bites are loaded. In this tutorial we will start first with the background. Then we will see the screensaver.


II. SETTING UP YOUR WORKSPACE
Assuming that "MainFolder" is the folder where you will put the final swf.

Create in "MainFolder" a new folder named "background". This is the folder where the background image files and one php file will be.
Create in "MainFolder" a new folder named "screensaver". This is the folder where the screensaver image files and one php file will be.
Create in "MainFolder" a new folder named "inc". This is the folder where you will put the const.inc.php file (and all the other include file you can use for your future website).

So you should have something like this:
MainFolder
  • background
  • inc
  • screensaver


III. THE PHP FILES 
a. config.inc.php
First, as all developers I suppose, we will create inside the MainFolder a "config.inc.php" file. This file is here to help development. In fact, all our php files will call this one to get the WEBSITE's PATH.
Here is the file:

<?php

//folder for include file

$C_INC = $C_ROOT."inc/";

//folder for background file

$C_BG = $C_ROOT."background/";

//folder for screensaver file

$C_SS = $C_ROOT."screensaver/";

?>


The $C_ROOT var will be sent by php file including the config.inc.php, it refers to the MainFolder folder. We will see this later on the tutorial.

b. const.inc.php
Now create a new "const.inc.php" php file inside the "inc" folder. This file will have all the constants needed (for our tutorial and for your future website).
Here is the file:

<?php

//all the valid extension for the background

$C_MYEXTENSION = array ( 'jpg', 'jpeg', 'png', 'gif', 'swf' );

//all the valid extensions for the screensaver

$C_MYEXTENSIONSS = array ( 'swf' );

?>


In this file we are creating two arrays. The first one is holding all the extension for the background file, the second for the screensaver. I hope it's enough clear. I could put all in one file, but I think it is better this way: one php file for path, another one for constants used.

c. listfile.php
Ok, now we need to make a php file that will check inside a folder all the file, then keep only the file with the extension we want (those which are define in "const.inc.php"). The php file will finally echo the name of the file to the flash movie.
We will save the file in the folder "background" with the name "listfile.php".

<?php

//give the relative path of the root (MainFolder) of the
//website.

$C_ROOT = "../";

//get the config.inc.php then know all the relative path from the root.

include_once $C_ROOT."config.inc.php";

//include all the constant like our array of extension

include_once $C_INC."const.inc.php";

//opening the current folder

$handle=opendir(".");

//make an empty array that will get our background
//filename of the actual folder

$ourFile = array();

// while next file in the folder exist

while ($file = readdir($handle)) {
       
// don't check ./ and ../
       
if ( ($file != ".") && ($file != "..")) {
 
// split the string in an array, substring
// separate by a '.'

$ext = explode('.', $file);
 
// how many value in the array
 
$howmany = count(ext);
 
// redefine the var ext. Get the extension ie the
// last substring
 
$ext = strtolower($ext[$howmany]);

// check the extension of the file. If the
// extension is on our array (const.inc.php file)

// then add the filename to our array of
// background
 
 if( in_array ($ext, $C_MYEXTENSION) ) array_push($ourFile, $file);
       
 };

};

// selecting one of the value of the ourFile array, means
// that we are taking one of the background file name

$theFile = $ourFile[array_rand($ourFile)];

//deleting the handler

closedir($handle);

//send it to flash

echo "&thefile=".$theFile."&";

?>


Ok, so now we have a php file that echo a filename, like eg:
"Myfirstpic.jpg".

This file is inside the folder "background", so you need to put some background image files inside the "background" folder.

Then test the file by opening your browser like:
localhost/your_Path_to_MainFolder_From_the_Root_of_The_Webserver/background/lisfile.php

You should have something like:
&thefile=Myfirstpic.jpg&


For the screensaver it's the same thing.
Just copy this file, and some image files inside the "screensaver" folder.

Ok so all the php side is done :)

Let's go in Flash now !!! (I was impatient too)