agentjozef Posted January 8, 2012 Share Posted January 8, 2012 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254586-php-xml-from-the-contents-of-a-directory/ Share on other sites More sharing options...
ignace Posted January 8, 2012 Share Posted January 8, 2012 What have you tried so far? Because since you can't answer your own question I assume you just copied the above code from somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/254586-php-xml-from-the-contents-of-a-directory/#findComment-1305492 Share on other sites More sharing options...
agentjozef Posted January 8, 2012 Author Share Posted January 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254586-php-xml-from-the-contents-of-a-directory/#findComment-1305494 Share on other sites More sharing options...
ignace Posted January 8, 2012 Share Posted January 8, 2012 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(); Quote Link to comment https://forums.phpfreaks.com/topic/254586-php-xml-from-the-contents-of-a-directory/#findComment-1305496 Share on other sites More sharing options...
agentjozef Posted January 8, 2012 Author Share Posted January 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254586-php-xml-from-the-contents-of-a-directory/#findComment-1305498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.