First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Nuno Mira | Website: http://www.nunomira.com/

PHP:

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);
fclose($fp);

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:

	header("Location:$file");
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:

	http://www.mydomain.com/list-files.php?action=create
	http://www.mydomain.com/list-files.php?action=view

» Level Intermediate

Added: 2011-02-28
Rating: 7.5 Votes: 2
(10 being the highest)
» 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!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.