PHP: How do I loop through every XML file in a directory?
Posted
by celebritarian
on Stack Overflow
See other posts from Stack Overflow
or by celebritarian
Published on 2010-03-20T22:41:27Z
Indexed on
2010/03/20
22:51 UTC
Read the original article
Hit count: 358
Hi!
I'm building a simple application. It's a user interface to an online order system.
Basically, the system is going to work like this:
Other companies upload their purchase orders to our FTP server. These orders are simple XML files (containing things like customer data, address information, ordered products and the quantities…)
I've built a simple user interface in HTML5, jQuery and CSS — all powered by PHP.
PHP reads the content of an order (using the built-in features of SimpleXML) and displays it on the web page.
So, it's a web app, supposed to always be running in a browser at the office. The PHP app will display the content of all orders. Every fifteen minutes or so, the app will check for new orders.
How do I loop through all XML files in a directory?
Right now, my app is able to read the content of a single XML file, and display it in a nice way on the page.
My current code looks like this:
// pick a random order that I know exists in the Order directory:
$xml_file = file_get_contents("Order/6366246.xml",FILE_TEXT);
$xml = new SimpleXMLElement($xml_file);
// start echo basic order information, like order number:
echo $xml->OrderHead->ShopPO;
// more information about the order and the customer goes here…
echo "<ul>";
// loop through each order line, and echo all quantities and products:
foreach ($xml->OrderLines->OrderLine as $orderline) {
echo "<tr>\n".
"<li>".$orderline->Quantity." st.</li>\n".
"<li>".$orderline->SKU."</li>\n";
}
echo "</ul>";
// more information about delivery options, address information etc. goes here…
So, that's my code. Pretty simple. It only needs to do one thing — print out the content of all order files on the screen — so me and my colleagues can see the order, confirm it and deliver it.
That's it.
But right now — as you can see — I'm selecting one single order at a time, located in the Order directory. But how do I loop through the entire Order directory, and read aand display the content of each order (like above)?
I'm stuck. I don't really know how you get all (xml) files in a directory and then do something with the files (like reading them and echo out the data, like I want to).
--> I'd really appreciate some help. I'm not very experienced with PHP/server-side programming, so if you could help me out here I'd be very grateful.
Thanks a lot in advance!
// Björn (celebritarian at me dot com)
© Stack Overflow or respective owner