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?

似乎当我在'/ var / www / cal / attach /'目录中有多个文件时,它只反复从第一个文件中提取元素。我是否需要以某种方式清除元素以使其正常工作?我要做的是让脚本经过多个* .htm文件,并将文件中的数据解析为$ value [x],我可以在以后调用。然而,相同的值显示多个文件......出了什么问题?

<?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";

}

?>

1 个解决方案

#1


0

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

我建议使用SPL中的目录迭代器对象。下面的示例使用递归的示例,但您不必是递归的。

$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";

更多相关文章

  1. 无法使用dropzone上传多个文件
  2. 关注:PHP文件目录和文件本身的操作
  3. php exec文件从终端运行,而不是从浏览器运行
  4. log4php将不同级别的日志打印到不同的日志文件中
  5. PHP - 在外部服务器上读取文件?
  6. 仅在LARAVEL或AJAX中提交表单时,无需用户身份验证即可将文件上载
  7. PHP ZipArchive 实现压缩解压Zip文件
  8. 纯php文件不加结束标记的好处
  9. 大文件从指定行开始读取

随机推荐

  1. Android 开发时遇到的两个模拟器问题
  2. 解析Android如何利用Handler
  3. Android官方入门文档[3]构建一个简单的用
  4. TextVview的属性
  5. Android Studio中Gradle使用详解
  6. Android OnTouchListener实时监听触点坐
  7. android 下载文件注意事项
  8. android 如何判断软件是否第一次运行?
  9. Android TextView中链接(link)点击事件的
  10. Android 动画旋转效果