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();	
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();	

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.