开发者

PHP: How do I loop through every XML file in a directory?

开发者 https://www.devze.com 2022-12-24 15:54 出处:网络
I\'m buildin开发者_运维技巧g a simple application. It\'s a user interface to an online order system.

I'm buildin开发者_运维技巧g 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).


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
     }

}


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,

   public function parse () {
    $files = File::allFiles(storage_path('xml'));

    //dd($files);

    foreach($files as $filename) {
        //dd($filename);
        $xml_file = file_get_contents($filename, FILE_TEXT);      
        $xml = simplexml_load_string($xml_file, "SimpleXMLElement", LIBXML_NOCDATA);
        $json = json_encode($xml);
        $array[] = json_decode($json,TRUE);
        //dd($array);
    }
    dd($array);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消