PHP:
If you dont have access to a PHP enabled server you can easily install one locally.
WampServer and XAMPP are the most common ones. Youll 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 well 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 youd like to specify another directory, you can:
$directory = 'images/';
$files = glob("{$directory}*.jpg");
Make sure that you dont 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 dont know exactly whats 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, youll need to set the headers with the correct mime type:
header("content-type: text/xml");
echo $xml;
On the other hand, if youre creating the XML file, youll use file_put_contents():
file_put_contents($file, $xml);
This function became available with PHP 5 only. If you're using PHP 4 youll 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 doesnt 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 youll 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-03-17 Rating: 1 Votes: 1 |
| » 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. |
| Download (814 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


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