The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

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 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
    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 Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012