php - loop through a directory files and run script

32

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

Answer

Solution:

Although your question was answered in the comments already, I advise against usingglob(). 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. Withglob() 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 recursiveglob() 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 onlibc implementation that can change for any reason?

  5. The patternsglob() 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 includesGLOB_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.

People are also looking for solutions to the problem: PHP: Syncing Data with Amazon S3

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.