First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Nuno Mira | Website: http://www.nunomira.com/


If you don’t have access to a PHP enabled server you can easily install one locally.
WampServer and XAMPP are the most common ones. You’ll get Apache, PHP and MySQL so this may be the entrance to a whole new world that you may never leave.

In order to read the contents of the directory we’ll use the glob() function.
One line of code does all the heavy work for you. This function returns an array with all the files’ names.
(The sample images were found by preforming this search. They are all the same size: 800x600)

Lets grab all the files with a .jpg extension in the current directory:

$files = glob("*.jpg");

If you have more than one extension, you can make use of the second parameter. In this case we use the GLOB_BRACE constant to retrieve all the .jpg, .jpeg, .gif and .png files:

$files = glob(".*{jpg,jpeg,gif,png}", GLOB_BRACE);

If you’d like to specify another directory, you can:

$directory = 'images/';
$files = glob("{$directory}*.jpg");

Make sure that you don’t add any extra spaces:

$files = glob("$directory*.{jpg,jpeg,gif,png}", GLOB_BRACE); // correct
$files = glob("$directory*.{jpg, jpeg, gif, png}", GLOB_BRACE); // incorrect
Also, extensions are case sensitive, so, if you don’t know exactly what’s in the folder, you may want to add both lower upper case extensions:

$files = glob("$directory*.{jpg,jpeg,gif,png,JPG,JPEG,GIF,PNG}", GLOB_BRACE);
All you have to do is start building the XML by looping through all the elements of the returned array:

$xml = '';
$xml .= '';
foreach ($files as $img)
	$xml .= "";
$xml .= '';
If you want to output the XML, you’ll need to set the headers with the correct mime type:

header("content-type: text/xml");
echo $xml;
On the other hand, if you’re creating the XML file, you’ll use file_put_contents():

file_put_contents($file, $xml);

This function became available with PHP 5 only. If you're using PHP 4 you’ll have two extra lines of code:

$fp = fopen($file, 'w');
fwrite($fp, $xml);

Notice that if the file exists, it will have to have write permissions.
If the file doesn’t exist, the directory will have to have write permissions.

After the file is created you may also want to be redirected to it:


In the PHP file you’ll find the code that allows you to either create a new XML file or view the output content using a variable action in the url:


» Level Intermediate

Added: 2011-03-17
Rating: 1 Votes: 2
» Author
Nuno Mira has been a Flash Developer for 9 years. He loves teaching, and learning. When he isn't coding he may be surfing or snowboarding.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.