provide latest xml file php

70 Views Asked by At

maybe someone can help me, i provide xml files witch are generated from a PHP DB query and each xml file has a unique name. Now i want to prepare a function like "get the latest xml file" but I don't know whats the best way!

$xml = simplexml_load_file('test.xml');

I found this function but there i have to know the exact name! or ist something like this possible:

$xml = simplexml_load_file('test.php');

and in the test.php i have a function to get the last name, but how to i provide the xml data? Some keywords how i can find a solution in google would be very helpful!

2

There are 2 best solutions below

0
On BEST ANSWER

here the finish solution that worked for me

i protected the directory with .htaccess and inside i store all my generated xml files and also the getLastXml.php file!

the getLastXml.php

function get_last_file() {

    $lastFileTime = 0;
    foreach (glob("*.xml") as $filename) {
        if ($lastFileTime<filemtime($filename))
            {
                $lastFileTime = filemtime($filename);
                $lastFileName = $filename;
            }
        }

    return $lastFileName;
}

$lastXmlFile = get_last_file();
header ("Content-Type:text/xml");
echo file_get_contents($lastXmlFile);

the functions get_last_file() returns the name of the latest created xml file and

 header ("Content-Type:text/xml");

displays xml in the php file

echo file_get_contents($lastXmlFile);

loads the content of the xml file and display it


simplexml_load_file("http://username:passwort@urlToTheDirectory/getLastXml.php");

loads the xml data with

0
On

The first parameter to that function is a string of the filename. The file should be the XML file to load, so you cant use another php file.

http://php.net/manual/en/function.simplexml-load-file.php

So you need to get the filename as a string first by using a variable. You should be able to copy the code in your test.php file, then save the filename instead of echoing it out. Then you use that variable when loading the xml file.

e.g.

function get_latest_filename()
{
    //contents of your test.php file should set this variable
    $latest_filename = 'the_latest_file.xml';
    return $latest_filename;
}

$latest = get_latest_filename();
$xml = simplexml_load_file($latest);