Awena
Awena

Reputation: 1022

php - loop through a directory files and run script

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... 
 }

Upvotes: 2

Views: 657

Answers (1)

rr-
rr-

Reputation: 14811

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.

Upvotes: 1

Related Questions