PHP:如何循环遍历目录中的每个 XML 文件?
我正在构建一个简单的应用程序。它是在线订单系统的用户界面。
基本上,系统将像这样工作:
其他公司将他们的采购订单上传到我们的 FTP 服务器。这些订单是简单的 XML 文件(包含客户数据、地址信息、订购的产品和数量等内容......)
我用 HTML5、jQuery 和 CSS 构建了一个简单的用户界面 - 全部由 PHP 提供支持。
PHP 读取订单内容(使用 SimpleXML 的内置功能)并将其显示在网页上。
所以,它是一个网络应用程序,应该始终在办公室的浏览器中运行。 PHP应用程序将显示所有订单的内容。每隔十五分钟左右,应用程序就会检查新订单。
如何循环遍历目录中的所有 XML 文件?
现在,我的应用程序能够读取单个 XML 文件的内容,并以良好的方式在页面上显示它。
我当前的代码如下所示:
// 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) {
"<li>".$orderline->Quantity." st.</li>\n".
"<li>".$orderline->SKU."</li>\n";
}
echo "</ul>";
// more information about delivery options, address information etc. goes here…
所以,这就是我的代码。很简单。它只需要做一件事——在屏幕上打印出所有订单文件的内容——这样我和我的同事就可以看到订单、确认并交付。
就是这样。
但现在 - 正如您所看到的 - 我一次选择一个订单,位于订单目录中。但是我如何循环遍历整个订单目录,并读取并显示每个订单的内容(如上)?
我被困住了。我真的不知道如何获取目录中的所有(xml)文件,然后对这些文件执行某些操作(例如读取它们并回显数据,就像我想要的那样)。
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) {
"<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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 glob 函数:
Try the glob function:
任何想要在 laravel storagege/xml 中存储的人,他们都可以从那里获取整个 xml 文件,并使用 Laravel 中的以下函数将其更改为 json,但只需稍作修改即可在核心 PHP 中使用,
Anyone who looking to store in laravel storagege/xml,They could get from there entire xml file and change it to the json using below function in Laravel but with small modification can used in core PHP,