PHP:如何遍历目录中的每个 XML 文件? [英] PHP: How do I loop through every XML file in a directory?

查看:24
本文介绍了PHP:如何遍历目录中的每个 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:

  1. 其他公司将他们的采购订单上传到我们的 FTP 服务器.这些订单是简单的 XML 文件(包含客户数据、地址信息、订购的产品和数量……)

  1. 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…)

我用 HTML5、jQuery 和 CSS 构建了一个简单的用户界面 - 全部由 PHP 提供支持.

I've built a simple user interface in HTML5, jQuery and CSS — all powered by PHP.

PHP 读取订单内容(使用 SimpleXML 的内置功能)并将其显示在网页上.

PHP reads the content of an order (using the built-in features of SimpleXML) and displays it on the web page.

因此,它是一个网络应用程序,应该始终在办公室的浏览器中运行.PHP 应用程序将显示所有订单的内容.每隔 15 分钟左右,该应用就会检查新订单.

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.

现在,我的应用能够读取单个 XML 文件的内容,并在页面上以漂亮的方式显示它.

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.

我当前的代码如下所示:

// 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>
".
    "<li>".$orderline->SKU."</li>
";
}
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.

就是这样.

但是现在——正如你所看到的——我一次选择一个订单,位于订单目录中.但是如何遍历整个 Order 目录,并读取并显示每个订单的内容(如上)?

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

我被卡住了.我真的不知道你是如何获得一个目录中的所有 (xml) 文件,然后对这些文件做一些事情(比如读取它们并回显数据,就像我想要的那样).

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).

推荐答案

试试 glob 功能:

$files = glob("Order/*xml");

if (is_array($files)) {

     foreach($files as $filename) {
        $xml_file = file_get_contents($filename, FILE_TEXT);
        // and proceed with your code
     }

}

这篇关于PHP:如何遍历目录中的每个 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆