views:

163

answers:

1

It seems that when I have multiple files within the '/var/www/cal/attach/' directory, it only extracts the elements from the first file over and over again. Do I need to clear out the elements somehow to get this to work properly? What I'm trying to do is have the script go through multiple *.htm files, and parse data from the files into $value[x] where I can call at a later time. Yet the same values show up for multiple files... Whats going wrong?

<?php
error_reporting(0);

$today = date("Y-m-d");

foreach (glob("/var/www/cal/attach/*.htm") as $filename) {
$file = $DOCUMENT_ROOT. "$filename";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);


$elements = $doc->getElementsByTagName('td');
if (!is_null($elements)) {
  foreach ($elements as $element) {
    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
        $value[] = $node->nodeValue. "\n";
    }
  }
}
echo "date,test,response time,availability,$filename\n";
echo $today . "," . trim($value[25]) . "," . trim($value[26]) . "," . trim($value[27]) . "\n";
echo $today . "," . trim($value[31]) . "," . trim($value[32]) . "," . trim($value[33]) . "\n";
echo $today . "," . trim($value[37]) . "," . trim($value[38]) . "," . trim($value[39]) . "\n";

}

?>
A: 

I recommend using directory iterator object from SPL. The example below uses the recursive one, but you don't have to be recursive.

$dir_iterator = new RecursiveDirectoryIterator("/path");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wish


$size = 0;
foreach ($iterator as $file) {
    if ($file->isFile()) {
        echo substr($file->getPathname(), 27) . ": " . $file->getSize() . " B;        modified " . date("Y-m-d", $file->getMTime()) . "\n";
         $size += $file->getSize();
    }
}

 echo "\nTotal file size: ", $size, " bytes\n";
Good Time Tribe