I have a simple XML parser that i want to run sequentially all files within a directory, my question is how to loop through all xml files and run the script one after another. Instead of doing so manually.
$string = file_get_contents('epgs/file1.xml'); // loop through all files
$xml = new SimpleXMLElement($string);
$events=$xml->xpath("/DVB-EPG/Service/Event");
if ($events) {
foreach ($events as $event) {
$id = $event["id"];
$start_date = $event["start"] ;
$name = $event->ShortEventDescriptor->EventName ;
$text = $event->ShortEventDescriptor->Text;
$values = array (
':event_id'=>$id,
':event_name'=>$name,
':event_desc'=>$text,
':start_date'=>$start_date
);
$stmt = $dbh->prepare ($sql);
$stmt->execute($values);
}
}
Where the directory epgs has multiple files : file1.xml, file2.xml, file3.xml ect..
ANSWER
$files = glob("epgs/*.xml");
foreach ($files as $file) {
//script...
}
glob("epgs/*.xml")?