2

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... 
 }
2
  • 3
    glob("epgs/*.xml")? Commented Apr 2, 2014 at 9:40
  • You should post this your self and mark it as the answer so this question appears answered. Commented Jun 6, 2014 at 21:41

1 Answer 1

1

Although your question was answered in the comments already, I advise against using glob(). You can use SPL iterators instead:

<?php
$folder = __DIR__;
$pattern = '/^.*\.xml$/';

$iterator = new RecursiveDirectoryIterator($folder);
$iterator = new RecursiveIteratorIterator($iterator);
$iterator = new RegexIterator($iterator, $pattern);
$files  = [];
foreach ($iterator as $file) {
    $files []= $file->getFilename();
}
var_dump($files);

Even though the code is much bigger, I still find it more useful. The benefits are as follows:

  1. You get absolute paths, period. With glob() that is the case only if your original pattern is absolute as well. That's not very obvious, isn't it?

  2. Actually, you get a lot more information that absolute path - you can check file size, its owner etc. Check documentation on SplFileInfo.

  3. The second you find yourself in need of handling recursive pattern, e.g.:

    folder/*.xml
    folder/branch/*.xml
    folder/even/deeper/*.xml
    

    ...you'll realize that there's no built-in recursive glob() in PHP.

  4. glob() supports many more patterns than simple *. Unfortunately, they are barely documented:

    The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

    Are you seriously wanting to depend on libc implementation that can change for any reason?

  5. The patterns glob() actually supports seem to be described best on some random blog™:

    * (an asterisk)

    Matches zero of more characters.

    ?

    Matches exactly any one character.

    [...]

    Matches one character from a group. A group can be a list of characters, e.g. [afkp], or a range of characters, e.g. [a-g] which is the same as [abcdefg].

    [!...]

    Matches any single character not in the group. [!a-zA-Z0-9] matches any character that is not alphanumeric.

    \

    Escapes the next character. For special characters, this causes them to not be treated as special. For example, \[ matches a literal [. If flags includes GLOB_NOESCAPE, this quoting is disabled and \ is handled as a simple character.

  6. To me, using regular expressions is much more readable. People are much less likely to need to refer to documentation if they see simple regex.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.