XML Generator DIR

XML Generator DIR is an XML generator that will make a lot easier for you to create XML configuration files. It is focused in the creation of configuration files for image galleries, music players and for any other stuff where you need to list all files in a directory in a simple way.

This script uses a model file to give you freedom defining your XML file. You can create practically any structure you want and let the script populate it for you.

If you need to read records from a database instead of directories in a filesystem you should check the XML Generator DB instead.

The XML model

For this script to work you need to create a model according to your needs. In this model you use placeholders to customize the output. Take a look at this sample:

<?xml version="1.0" ?>
<images date="{callback:today}">
	<image id="{id}" name="{pathname}">
  		<caption>{input:caption}</caption>
  		<format>{callback:discoverOrientation}</format>
  		<size>{callback:filesize}</size>
	</image>
</images>

This XML is a template of a one used for creating a configuration file of images. Look that we have the root tag images and its child tag image. The XML Generator 2.0 will detect it and will duplicate the child node for every file found in the directory. It will also create an XML file for each supplied directory. The placeholders {…} will be replaced by their meanings.

Placeholders

This script supports six different placeholders that you can use in your model. Here is a list of all of them and their explanation:

  • {filename} - If used in any child node it’ll be replaced by the name of each file listed in the directory. If used in the root node it’ll be replaced by the name of the XML file.
  • {dirname} - It’ll be replaced by the name of the current directory being read.
  • {pathname} - Again, if used in any child node it’ll be replaced by the pathname of each file listed in the directory. If used in the root node it’ll be replaced by the pathname of the XML file.
  • {id} - It’s only available for child nodes and it’ll be replaced by an auto-increment number.
  • {input:info} - This placeholder causes the script to ask for user input when running through CLI. The info value should be any informative text that you want to display when asked for data input. If run through other interfaces, the script will replace it by an empty space instead.
  • {callback:function} - This placeholder will call the informed function passing the pathname as an argument. It’ll be replaced by the function returned value.

File Configurations

The last configuration step is to define some variables directly inside the script. Open the xmlgen2.php and edit from line 29 up to line 38. Here is what you will see:

28
29
30
31
32
33
34
35
36
37
38
// list of directories to be read ...
$directoryList = array('images');
 
// name of the XML file
$xmlFileName = 'images.xml';
 
// name of the XML model
$xmlModelName = 'model.xml';
 
// if the script should invert Windows slashes ('' to '/')
$invertSlash = true;

Running the Generator

Now the fun part! You can call the script from two different ways. You can call it through the webserver from the browser as you would normally do but you can also execute it through the command line interface (CLI).

Calling the script through the webserver gives you the option to pass a list of directories separated by commas to the script via the GET variable dirlist. These directories will be appended to the ones previously defined directly in the source code.

	http://localhost/images/xmlgen2_dir.php?dirlist=myphotos,landscape

If you execute the script through the CLI you gain the possibility of using the {input:} placeholder and interact with the processing. Also, you can pass the directories as the script arguments.

	$ php xmlgen2_dir.php myphotos landscape

Note: To run the above command on Windows you may need to add the php executable to your system path. To do that type: set path = %path%;c:\directory\of\php\; in your command prompt then restart it.

If you have any doubt or suggestion leave a comment that I will be glad to help.

Files:

XML Generator DIR
model_dir.xml

52 Responses to “XML Generator DIR”

  1. Justin Rhoades Says:

    This is killer, many thanks!

  2. Justin Rhoades Says:

    For anyone trying to use the php5 CLI installed by Marc Liyanage’s Mac OS X php package: be sure to use “/usr/local/php5/bin/php” as the path when running the PHP binary.

  3. fromvega Says:

    Tks for the information Justin!

  4. junal Says:

    Nice information Justin!

  5. JSin Says:

    Great job!!!
    Works great on my server running php5 but when I popped it on a server running php 4.4 it hangs up at the “try” block. Is there anyway around this?

  6. fromvega Says:

    Hello JSin,

    Yeah there is a way around it but not so easy. It would be to rewrite a lot of the code since PHP 4 doesn’t have the iterators I’m using neither the same XML library, sorry.

    Regards

  7. Rayn Says:

    Big THX for this generator!!
    I have very poor knowledge in php but some how i set up wamp sever, he is runnig on php 5.2.5 but when i run script i get this error - Parse error: syntax error, unexpected ‘

  8. fromvega Says:

    Hello, try to check if your XML is valid, if it’s correct. If it’s not the case try to check the PHP source code, maybe something has happened while you were editing it.

  9. ranmal Says:

    Hello please help i’m getting error

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\images\xmlgen2_dir.php on line 32

  10. fromvega Says:

    Hello ranmal,

    First of all you need PHP 5 to be able to run this script. Second, be sure the code isn’t messed up, try to download again and see what happens!

    Bye!

  11. Aysseline Says:

    Hi, I’m trying your files on XP/xampp and get this errors:
    Warning: DOMDocument::load() [function.DOMDocument-load]: I/O warning : failed to load external entity “file:///C%3A/xampp/htdocs/test/model.xml” in C:\xampp\htdocs\test\xmlgen2_dir.php on line 50

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test\xmlgen2_dir.php on line 62

    Fatal error: Call to a member function saveXML() on a non-object in C:\xampp\htdocs\test\xmlgen2_dir.php on line 73

    And I don’t understand (Maybe result for the first warning) this:
    “To run the above command on Windows you may need to add the php executable to your system path. To do that type: set path = %path%;c:\directory\of\php\; in your command prompt then restart it.”

    Do I need to make a change in my php.ini ?

    Thanks

  12. fromvega Says:

    Hi Aysseline,

    First of all, the first error indicates that the path to your file is wrong. The following errors are consequence of it. Fixing it will solve your problems.

    Now, about the windows system path, it’s just to be able to call the php executable from wherever you are. If php.exe isn’t in your system path you would need to execute php from its own dir.

    Bye!

  13. glyf Says:

    Thanks for this script, works great. One modification that is useful if you want to run the script repeatedly is to edit the write destination so it doesn’t write into the image directory itself. That way, when the script is called a second time, it doesn’t generate a reference to the previous xml file in the new xml file.

    Thanks!

  14. Tony Coyle Says:

    Nice work, script works well. Would be handy to have a config flag/array to return only certain file types instead of all files.

  15. Benoit Borrel Says:

    Hello,

    Thanks for this great script which saved me a lot of development time. I hacked it a bit to restrict images listing on specified image types only:

    [line32, added 2 lines]
    // list of researched image file types
    $imageTypeList = array(’gif’,'jpg’,'png’);

    [line 153, modified 1 line]
    if($file->isFile() AND in_array(end(explode(”.”, $file)),$imageTypeList)) {

    Thanks.

  16. Ivan V. Says:

    Hi , I was wondering ,if this php script can list the subfolders , in one main folder?
    Like this:$directoryList = array(’images’); , so when I pass in the images folder , it lists all subfolders , and images inside root folder (i.e ‘images’).
    I would like to use this kind of script in combination with flash. So the flash always loads this same xml.
    btw. I’m a total newbie for php.
    Thanks.

  17. fromvega Says:

    Hello Ivan!

    Not in the present state but everything is possible, we just need to write the code ;-)

    But unfortunately I don’t have much time now to implement this feature.

    Bye.

  18. Ivan V. Says:

    Thanks for the quick respone , I hope you implement this kind of feature in future.
    Bye.

  19. Matjaz Says:

    Thanks, very usefull for flash galleries. ;)

  20. saç bakımı Says:

    thank

  21. Webweaver Says:

    I just wanted to say what an awesome program you have saved me so much time with my projects.
    Well done in this awesome work ;)

  22. Webeaver2000 Says:

    Hi again, could someone please help me i need to get this code :

    {input:caption}
    {callback:discoverOrientation}
    {callback:filesize}

    To out put like this eg:

    Nitro1 video.flv
    webweaver
    video.flv

    Nitro2 video.flv
    Underground
    video.flv

    Nitro3 video.flv
    Underground
    video.flv

    Could someone please help me with the code, to get the output correct?

    it would be much appreciated
    thanks ;)

  23. fromvega Says:

    Hey Webeaver2000,

    I was blocking your comments because they aren’t kind comments/questions. You are posting a huge bunch of code! That’s not how it works mate, sorry.

    I’m not sure if I understand your problem. But you should be aware that this code is for XML files. It needs an XML file structure to work. It won’t work for plain text files if its what you are trying to do.

    If you need more help, please be kind to us and describe your problem more clearly and without pasting all your source code. ;)

  24. Webeaver2000 Says:

    Lol, sorry, i just thought seeing my code you can see how its structured. Sorry, wont happen again,
    I got it running now by tweaking a few things i still am having a few problems but i think i can knuckle them out now,
    Thanks again for a awesome script ;)

  25. Webeaver2000 Says:

    hi again ;), I still have this 1 problem. I would like to change the image to track in the model_dir.xml file so the xml file outputs this format

    I have really tried everthing lol i think I hope this is clear information its just when i change it on the model_dir i get errors and i dont know where to change the info so it matchs with the XML Generator DIR
    your help would be great , cheers ;)

  26. Webeaver2000 Says:

    the changing of the “image” to “Track” and “tracklist” in model_dir

  27. kleklezin Says:

    Hello man first of all thx for the script. another question… i have a image upload site and im trying to make a twist here since my galleries are in flash. So my ideia is… a upload image form, then when you upload the images your script is called and creates the xml with the image names and folder, but since every upload is different each time the image should go to a special folder, that the userl will define.
    Any clues on how do i do that? hehe thx man

  28. mike Says:

    I have customised my the script as shown :

    // list of directories to be read with no trailing slash
    $directoryList = array(’pictures’);

    // name of the XML file
    $xmlFileName = ‘gallery.xml’;

    // name of the XML model
    $xmlModelName = ‘model.xml’;

    I am assuming the model.xml is how i want my gallery.xml to look like.

    I am failing to create the gallery.xml. I get the error shown below.

    Strict Standards: Non-static method DOMDocument::load() should not be called statically in H:\p3t\phpappfolder\public_php\Photo Album\xml_convert2.php on line 50

  29. Robert Says:

    Thanks for the script, nice.

    I wrote one in Classic ASP and what it does is write out the resulting XML file to the browser.

    It looks like yours writes out to the images.xml file which seems to me to kind of defeat the purpose of dynamic calling and a sort of RESTful request from another server with (in Flash, crossdomain.xml privilages).

    The reason I need to use a PHP script this time around is that the images are on an Apache file.

    All I need to do is generate something compatible with Slideshow Pro, so a model like this,

    As you can see, the only thing I’m interested in is calling a .PHP file with a directory name, and getting back the results in the browser via XML.

    Flash can easily parse this, as well as any app. like HTTPTear which is good for RESTful access, or even just calling in FLEX.

    I find calling URLs with parameters and obtaining back XML results far easier than calling the URL with params, and then having to access an .xml file it generates.

    Can you help here?

    Thanks.

  30. fromvega Says:

    Hello Robert,

    What you want is pretty simple, just change this:

    $dom->save($directory.’/’.$xmlFileName);

    to this:

    echo $dom->saveXML();

    Hope this works as expected!

  31. fromvega Says:

    Mike, you can try to change all DOMDocument::load() calls to something like the following:

    $dom = new DOMDocument();
    $dom->load(’thenameofthefile.xml’);

  32. Aleksandra-Maria Says:

    Your script is great and I’ve learned a lot! Tnx

    I have customised Your script a little and I have created a subfolder “photos” with my images inside –> so the file structure is next:
    +Gallery (dir)
    xmlgen2.php
    model_zeko.xml
    +photos (dir)
    1.jpg
    2.jpg

    The problem is that, although there’s no “Thumbs.db” in subfolder “photos”, when I open generated xml file (in subfolder “photos”), the “Thumbs.db” file is listed as one of the files (as the last one in xml list) … Don’t know where’s the problem…

    Can You give an advice about that?

    Tnx, Alex

  33. Aleksandra-Maria Says:

    My previous post doesn’t show the file (and folder) structure as it is –> so to explain: Gallery is the main folder –> I’ve put inside: xmlgen2.php (script file), model_Zeko.xml (model file) and subfolder photos (with images inside and the target folder for generated xml)

    Alex

  34. fromvega Says:

    Hello Aleksandra-Maria, the Thumbs.db is by default a hidden file in Windows (you can change that somewhere in the Control Panel). So even if you can’t see it, PHP still can.

    But you can edit the code and instruct it to skip the Thumbs.db file:

    // after this
    if($file->isFile()){

    // add this line
    if($file->getFilename() == ‘Thumbs.db’) continue;

    It should work!

  35. fromamr Says:

    The saveXML() problem is still existing. Can anybody helpout for this.
    I used
    $var=dom->saveXML();
    also i tried with
    echo dom->saveXML

  36. harga komputer Says:

    Thanks for this script, works great. Thanks, very usefull.

  37. allen Says:

    hellow fromvega

    i was just wondering if you could fix my problem….
    i want the XML to look like this, i just erase the opening tag to be able to show the code…

    vids>

    vid url=”patandme.flv” description=”patandme” />
    vid url=”2.flv” description =”2″ />
    vid url=”3.flv” description=”video” />
    vid url=”Emotero.flv” description=”emo boy” />

    /vids>

    your help will be appreciated… thankyou

  38. jhon Says:

    hi fromvega

    please help me, i had copied the XML Generator DIR to php, and model_dir.xml to xml, when i run the xmlgen2_dir in my browser, nothing is happening, just white space and no XML was generated. could you help me please

  39. Erik A. Says:

    hi fromvega. Thanks for the script, its great. I’m having a similar problem as Aleksandra-Maria with it adding a hidden file to the xml. I used the code you provided as follows:

    // after this
    if($file->isFile()){

    // add this line
    if($file->getFilename() == ‘.DS_Store’) continue;

    For some reason it still includes the .DS_Store file as the first item in the xml. Any ideas?

  40. Glenn B. Says:

    I am trying to get a grasp on this, but am new to XML scene.

    I am creating a gallery for a friend with thumb_nails_path, image_path, and link.

    I am a little confused as to how to assign multiple paths. Any thoughts?

    Thank you in advance.

  41. BEA Says:

    I did have a good look around, but missed that thread.

    works perfectly…

  42. hkwebhosting Says:

    Thanks for this script, works great. Thanks, very usefull.

  43. Nigel Says:

    Fantastic script thank you… I’ve got it working but have a small problem which I can’t see how to solve as my php isn’t very good.

    The model.xml is as below but I only need it to repeat “” What do I need to do to make it go to that next level?

    Bijou Photo
    Photos from Site

  44. Nigel Says:

    Ahh it removed my tags. Grrrr.. Basically there I need 2 tags around the ones that repeat. So only the photo one repeats.

    tiltviewergallery
    photos

    photo imageurl=

  45. Martin Says:

    Erik A.

    //Replace
    if($file->isFile()){

    //With
    if($file->isFile() && $file->getFilename() != “file to hide”){

    Thanks for this wonderfull script.

  46. Pirmin Says:

    hey fromvega,
    thanks for the great code. i have a problem though. i want to use files named “1.swf”, “2.swf”, and so on. unfortunately the order in the xml-file generated is pretty much randomized. is there any way to sort the xml childnodes before saving the file? help would be much appreciated.
    thanks in advance and cheers

  47. Scotty Hancock Says:

    I just wanted to thank you very much for this illuminating article. I have already bookmarked your site, when I have more free time I am going to have to do some further research. Well back to my dreaming of Panama or back to the books - I wonder which one is going to win out. :)

  48. Sensa Says:

    Dont you plan to add a thumbnail creation function in your script? It would be really important for a flash gallery… :/

  49. Richard Says:

    Hi

    First off id like to say thank you for an awesome script !
    im using to make xml for slideshow pro, now i have it working except it is duplicating stuff in the xml that i dont need.

    eg this is what i need

    but its giving me this

    ?xml version=”1.0″?>

    see how its duplicating the album id tag ? how can i stop that ?

    Thanks

    Richard

  50. Salt Tablets Says:

    I am a neophyte to this XML issues but I am learning from your insightful post.

  51. Samy Says:

    Hi Fromvega,
    I just want to thank you first of all for this outstanding script I am new in php, but I have getting peaces from many places, looking for something like this. Although I haven’t runing or customize it yet I have checked the script by using phpDesigner and it trow me 3 errors, line 59 and 62 Trying to get property of non-object, line 79 call a member function saveXML() on a non-object.
    Is there any thing that I need to configure before I run it.
    How do you include a CDATA section? I will need it also. If you please help me on all these I will appreciate it.
    Thanks and blessings,

  52. Samy Says:

    Hi,
    Thank you so much for posting this interesting generator.
    I have only one question, how do you sort it in order?
    Thanks

Leave a Reply