Jump to content

PHP XML from the contents of a directory


agentjozef

Recommended Posts

I have this script below that makes an XML which displays the contents of a directory which contains image files.

After running the .php, the XML output looks like this:

<gallery>
<content src="./uploaded/images/Desert.jpg"/>
<content src="./uploaded/images/Penguins.jpg"/>
<content src="./uploaded/images/tux.png"/>
<content src="./uploaded/images/Jellyfish.jpg"/>
<content src="./uploaded/images/Chrysanthemum.jpg"/>
<content src="./uploaded/images/Tulips.jpg"/>
<content src="./uploaded/images/Koala.jpg"/>
</gallery>

I would like the output to be like:

<gallery>
<photos>
<photo>
<content src="./uploaded/images/Desert.jpg"/>
</photo>
<photo>
<content src="./uploaded/images/Penguins.jpg"/>
</photo>
<photo>
<content src="./uploaded/images/tux.png"/>
</photo>
<photo>
<content src="./uploaded/images/Jellyfish.jpg"/>
</photo>
<photo>
<content src="./uploaded/images/Chrysanthemum.jpg"/>
</photo>
<photo>
<content src="./uploaded/images/Tulips.jpg"/>
</photo>
<photo>
<content src="./uploaded/images/Koala.jpg"/>
</photo>
</photos>
</gallery>

Here is the script:

<?php
$path_to_image_dir = "./uploaded/images"; // path to your image directory

$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<gallery> 
</gallery>
XML;

$xml_generator = new SimpleXMLElement($xml_string);

if ( $handle = opendir( $path_to_image_dir ) ) 
{
    while (false !== ($file = readdir($handle))) 
    {
        if ( is_file($path_to_image_dir.'/'.$file) ) 
        {
           $image = $xml_generator->addChild('content'); 
           $image->addattribute('src', $path_to_image_dir.'/'.$file);        
        }
    }
    closedir($handle);
}

header("Content-Type: text/xml");
echo $xml_generator->asXML();	
?>

 

I looked around on the web and found some information on the topic, but what I found was confusing.

I tried this.

if ( is_file($path_to_image_dir.'/'.$file) ) 
        {
           $photos = new SimpleXMLElement('photos');
           $photo = $photos->addChild('photo');
           $image = $photo->addChild('content'); 
           $image->addattribute('src', $path_to_image_dir.'/'.$file);        
        }

but it left me with an empty page

The correct implementation would be below. Do you understand why? And why yours couldn't work?

 

$path_to_image_dir = "./uploaded/images"; // path to your image directory

$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<gallery> 
</gallery>
XML;

$xml_generator = new SimpleXMLElement($xml_string);
$photos = $xml_generator->addChild('photos');

if ( $handle = opendir( $path_to_image_dir ) ) 
{
    while (false !== ($file = readdir($handle))) 
    {
        if ( is_file($path_to_image_dir.'/'.$file) ) 
        {
           $photo = $photos->addChild('photo');
           $image = $photo->addChild('content'); 
           $image->addattribute('src', $path_to_image_dir.'/'.$file);
        }
    }
    closedir($handle);
}

header("Content-Type: text/xml");
echo $xml_generator->asXML();	

It seems that I put it into the loop, but it should have remained outside and the first element must be generated after being introduced by "

$xml_generator = new SimpleXMLElement($xml_string);"

But it still seems strange to me that I would get a blank page rather than some undesired result that I could manipulate until I could get what I want.

For example, I think it would give me something like:

<gallery>
<photos>
<photo>
<content src="./uploaded/images/tux.png"/>
</photo>
</photos>
<photos>
<photo>
<content src="./uploaded/images/zzz.png"/>
</photo>
</photos>
</gallery>

Getting nothing on the screen led me to a dead end.

Like I said, the help on this topic on the web was not clear to me.

Perhaps your response here will help someone else with my problem in the future.

Anyhow, I will remember this.

Thank you very much.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.