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.
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.
This script supports six different placeholders that you can use in your model. Here is a list of all of them and their explanation:
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; |
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.
27 Responses
Justin Rhoades
June 3rd, 2007 at 6:09 pm
1This is killer, many thanks!
Justin Rhoades
June 9th, 2007 at 10:49 pm
2For 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.
fromvega
June 10th, 2007 at 4:23 pm
3Tks for the information Justin!
junal
October 24th, 2007 at 4:21 am
4Nice information Justin!
JSin
November 12th, 2007 at 2:17 am
5Great 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?
fromvega
November 13th, 2007 at 10:35 pm
6Hello 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
Rayn
November 20th, 2007 at 9:35 am
7Big 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 ‘
fromvega
November 20th, 2007 at 10:19 pm
8Hello, 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.
ranmal
December 24th, 2007 at 5:57 am
9Hello 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
fromvega
December 25th, 2007 at 7:49 pm
10Hello 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!
Aysseline
January 2nd, 2008 at 3:17 am
11Hi, 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
fromvega
January 2nd, 2008 at 5:46 pm
12Hi 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!
glyf
January 17th, 2008 at 6:04 pm
13Thanks 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!
Tony Coyle
January 20th, 2008 at 9:33 pm
14Nice work, script works well. Would be handy to have a config flag/array to return only certain file types instead of all files.
Benoit Borrel
March 16th, 2008 at 8:30 pm
15Hello,
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.
Ivan V.
April 9th, 2008 at 11:44 am
16Hi , 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.
fromvega
April 9th, 2008 at 10:52 pm
17Hello 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.
Ivan V.
April 10th, 2008 at 8:59 am
18Thanks for the quick respone , I hope you implement this kind of feature in future.
Bye.
Matjaz
May 18th, 2008 at 7:24 pm
19Thanks, very usefull for flash galleries.
saç bakımı
June 14th, 2008 at 6:41 am
20thank
Webweaver
July 31st, 2008 at 1:07 pm
21I just wanted to say what an awesome program you have saved me so much time with my projects.
Well done in this awesome work
Webeaver2000
August 12th, 2008 at 3:10 am
22Hi 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
fromvega
August 15th, 2008 at 1:59 am
23Hey 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.
Webeaver2000
August 19th, 2008 at 4:05 am
24Lol, 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
Webeaver2000
September 10th, 2008 at 8:17 am
25hi 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
Webeaver2000
September 10th, 2008 at 8:22 am
26the changing of the “image” to “Track” and “tracklist” in model_dir
kleklezin
October 10th, 2008 at 10:27 am
27Hello 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
RSS feed for comments on this post · TrackBack URI
Leave a reply