Guldstrand Posted March 3, 2009 Share Posted March 3, 2009 Can someone please explain how to scan a directory and parse a xml file within the directory. templates/ ..template1/ ....images/ ....css/ ....index.php ....template.xml Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/147826-scandir-and-parse-xml-file/ Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 Use glob foreach (glob('*.xml')) as $file) { $fileData = file_get_contents($file); // parsing data here } Quote Link to comment https://forums.phpfreaks.com/topic/147826-scandir-and-parse-xml-file/#findComment-775905 Share on other sites More sharing options...
Guldstrand Posted March 4, 2009 Author Share Posted March 4, 2009 Use glob foreach (glob('*.xml')) as $file) { $fileData = file_get_contents($file); // parsing data here } Thanks for your fast reply. Can you please explain how to include "scandir"? I have this: $dir = 'templates'; $files = scandir($dir); foreach (glob("*.xml") as $filename) { echo "<p>$filename size " . filesize($filename) . "</p>\n"; } ..but it doesn´t output anything. Quote Link to comment https://forums.phpfreaks.com/topic/147826-scandir-and-parse-xml-file/#findComment-775914 Share on other sites More sharing options...
premiso Posted March 4, 2009 Share Posted March 4, 2009 The below doesn't use scandir but it does the same thing as scandir but it also filters what is in the folder. $dir = "templates"; foreach (glob($dir . "/*.xml") as $filename) { echo "<p>$filename size " . filesize($filename) . "</p>\n"; } That should scan the templates dir for xml files. For a scandir example: $dir = '/templates'; $files = scandir($dir); foreach ($files as $filename) { if (stristr($filename, ".xml") !== false) { $fileData = file_get_contents($file); // parsing data here } } As you can see, glob is easier as it pulls what you want, not everything including the kitchen sink. EDIT: Modified the scandir version to be correct. Quote Link to comment https://forums.phpfreaks.com/topic/147826-scandir-and-parse-xml-file/#findComment-775915 Share on other sites More sharing options...
Guldstrand Posted March 4, 2009 Author Share Posted March 4, 2009 Thanks again! But how can i parse/read the xml files if i have it like this: templates/ ..template1/ ....images/ ....css/ ....index.php ....template.xml ..template2/ ....images/ ....css/ ....index.php ....template.xml .... Quote Link to comment https://forums.phpfreaks.com/topic/147826-scandir-and-parse-xml-file/#findComment-775921 Share on other sites More sharing options...
Guldstrand Posted March 4, 2009 Author Share Posted March 4, 2009 Nevermind... I finally got it. I changed it to this: <?php $dir = 'templates/*'; foreach (glob($dir . "/template.xml") as $filename) { echo "$filename size " . filesize($filename) . "<br />\n"; ini_set('zend.ze1_compatibility_mode', '0'); $xml = simplexml_load_file($filename); print("<p> <img src=\"$xml->thumb\" alt=\" $xml->tempname \" title=\" $xml->tempname \" /><br /> Name: $xml->tempname<br /> Created by: <a href=\"mailto:$xml->email\">$xml->creator</a> </p>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147826-scandir-and-parse-xml-file/#findComment-775959 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.