Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


Results 1 to 11 of 11

Thread: Generating XML with PHP

  1. #1

    Generating XML with PHP

    Hi guys, I have a fairly simple question for those more familiar with PHP than myself. I'd like to read in a directory structure and write a simple XML file with a node for each file. This would be used for a flash gallery.

    Example input:

    Example output:

    <gallery path="images/">
    <image source="weddings/file1.jpg" />
    <image source="portraits/file2.jpg" />

    Super-simple, right? I have enough information on the actual writing of the file, I'm just not sure about how to read in the directory tree with paths intact. Thanks in advance for your pointers

  2. #2
    PHP Code:

    Function right($rightstring$rightlength) {
    substr($rightstring, -$rightlength));

    len($lenstring) {

    left($leftstring$leftlength) {
    mid($midstring$midstart$midend) {

    $imgdirname "photos/images";
    $imgdir opendir($imgdirname);
    While (
    False != ($imgfile ReadDir($imgdir) ) )
        If( (
    $imgfile != ".") And ($imgfile != "..") )

            If ((
    Right($imgfile4) == ".gif") Or (Right($imgfile4) == ".jpg") Or (Right($imgfile4) == ".png"))
    $width,$height,$type) = getimagesize("photos/images/$imgfile");
    $imgfilename[$q] = $imgfile;
    $imgfilesize[$q] = FileSize("photos/images/$imgfile");
    $imgfiletype[$q] = Right($imgfile3);
    $imgwidth[$q] = $width;
    $imgheight[$q] = $height;

    $sort_array = &$imgfilename;
    $sort_array as $key => $value)
    $fsize number_format($imgfilesize[$key] / 1024,2,'.',',');
    I wrote the code to do the exact same thing recently.

    That is a fairly advanced gallery, but the variables are pretty self explanitory to delete.

  3. #3
    Hot ****. This is why I like this forum! Thanks a million, Gazler! This will definately help propel my PHP knowledge!!

  4. #4
    Fabulous, I've rewritten my (far simplified) version. Here it is:
    //Courtesy of Gazler on the Kirupa.com forums!
    Function right($rightstring, $rightlength) {
    	return(substr($rightstring, -$rightlength));
    Function len($lenstring) {
    Function left($leftstring, $leftlength) {
    	return(substr($leftstring, 0, $leftlength));
    Function mid($midstring, $midstart, $midend) {
    return(substr($midstring, $midstart, $midend));
    $imgdirname = "images";
    $file=fopen($filename, "w") or die("Could not open $filename!");
    fwrite($file, "<gallery path=\"".$gallerypath."\">");
    $imgdir = opendir($imgdirname);
    echo "Reading directory $imgdirname ...\n";
    While (False != ($imgfile = ReadDir($imgdir) ) )
    	If( ($imgfile != ".") And ($imgfile != "..") )
    		If (Right($imgfile, 4) == ".jpg")
    			$imgfilename[$q] = $imgfile;
    			fwrite($file, "<image source=\"".$imgfile."\" caption=\"Generated by PHP and XML\" />");
    			echo "Wrote $imgfile \r";
    fwrite($file, "</gallery>");
    echo "XML update was successful. \n <a href=\"gallery.swf\">Link to your gallery";
    If you see anything glaringly wrong about this, let me know, but it works in all my tests, except for the \n escapes, they are not showing up.

    So, besides getting that to work, I have one question: Is there a way the script could automatically load a new page after the script executes? Thanks in advance!!

  5. #5
    using the html meta tags you could reload the page, or javascript, there is a PHP way, but that'll only work at the start of the page.

    Javascript method.
    PHP Code:
    <body onload="window.location.replace('gallery.swf')"
    By the way, you can delete the Left, Mid and Len functions, only right is used in that script.

  6. #6
    Excellent, that works perfectly. I've been tweaking like crazy and I've finally got a working script that generates this file:
    When I open that file in FireFox, it tells me:

    XML Parsing Error: junk after document element
    Location: http://www.epiphanus.net/current/tes...ld_gallery.xml
    Line Number 6, Column 1:
    <gallery path="gallery2/">

    When I look at the source, I see an XML file formatted just the way I want it, so I'm guessing this could have something to do with the \n line breaks I am writing to the file? If not, do you know what it might be?

    Thanks a million

  7. #7
    I wouldn't use \n when writing out the XML, if it still doesn't work, try changing a bit.

    PHP Code:
    photoset path="random">
    image path="7" />
    image path="asf" />
    photoset path="random2">
    image path="72" />
    image path="asf2" />

  8. #8
    I'm not sure what you mean... are you suggesting just leaving out formatting altogether? Right now I'm using \t in addition to \n , so perhaps those are just "dirty...?"

  9. #9
    I didn't use \n or \t in the formatting, here is my XML, it looks fine.



  10. #10
    I manually changed the XML to this and it works.

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    	<gallery path="gallery1/">
    		<image source="weddings04.jpg" />
    		<image source="ted_plane1.jpg" />
    	<gallery path="gallery2/">
    		<image source="weddings00.jpg" />
    		<image source="ted_plane2.jpg" />
    Strange error.

  11. #11
    Aha, I see. Properly formatted XML requires one and only one first child node, a single master parent. Blah, I say!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About kirupa.com Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012