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

查看:138
本文介绍了PHP:如何循环访问目录中的每个XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的应用程序。这是一个在线订单系统的用户界面。



基本上,系统将这样工作:


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


  2. 我已经建立了一个简单的用户界面HTML5,jQuery和CSS都是由PHP提供支持。


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


所以,它是一个网络应用程序,应该总是在办公室的浏览器中运行。 PHP应用程序将显示所有订单的内容。每十五分钟左右,应用程序将检查新订单。



如何循环访问目录中的所有XML文件?



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



我当前的代码如下所示:

  //选择我知道的随机订单存在于订单中目录:
$ xml_file = file_get_contents(Order / 6366246.xml,FILE_TEXT);
$ xml = new SimpleXMLElement($ xml_file);

// start echo基本订单信息,如订单号:
echo $ xml-> OrderHead-> ShopPO;
//有关订单的更多信息和客户进入这里...

echo< ul>;

//循环遍历每个订单行,并回显所有数量和产品:
foreach($ xml-> OrderLines-> OrderLine as $ orderline){
< ; li>$ orderline-> Quantity。st。< / li> \\\

< li>$ orderline-> SKU。< / li> \\\
;
}
echo< / ul>;

//有关送货选项,地址信息等的更多信息等等...

所以,那是我的代码。很简单它只需要做一件事 - 打印屏幕上所有订单文件的内容 - 所以我和我的同事可以看到订单,确认并交付。



就是这样。



但现在,您可以看到 - 我一次选择一个订单,位于订单目录中。但是如何循环遍历整个订单目录,并阅读并显示每个订单的内容(如上所述)?



我被卡住了。我真的不知道如何将所有(xml)文件放在一个目录中,然后用文件进行某些操作(比如读取它们并回显数据,就像我想要的那样)。



- >我真的很感激一些帮助。我对PHP /服务器端编程不是很有经验,所以如果你能帮助我在这里,我将非常感激。



提前感谢!



//Björn(celebrity at me dot com)

解决方案

欢迎来到SO。



尝试 glob 功能:

  $ files = glob(Order / * xml); 

if(is_array($ files)){

foreach($ files as $ filename){
$ xml_file = file_get_contents($ filename,FILE_TEXT);
//并继续执行代码
}

}


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

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

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

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

解决方案

welcome to SO.

Try the glob function:

$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天全站免登陆