Search code examples
phphtmlglob

Cant glob two types files without repeating itself


I know i have two foreach but when i assign glob of txt to a var it doesn't work. It should for each html file found write the correspondent txt file and so on, only one time. How can i shake this?

<?php

echo '<h2>Escolha o Esquema de MetaDados a utilizar</h2>';
echo '<table cellspacing="0" cellpadding="0" border="0" class="table">';
echo '<tr>';
echo '<th>Esquema de MetaDados</th>';
echo '<th>Descrição</th>';
echo '</tr>';


foreach (glob("Templates/Dinamico/*.html") as $filename) {
    foreach (glob("Templates/Dinamico/*.txt") as $txtname) {
        $fileH = basename($filename, ".html");
        $fileT = basename($txtname, ".txt");
        if ($fileH = $fileT) {
            $txt = file_get_contents($txtname);
            if ($filename != "Templates/Dinamico/formD1.html") {
                echo '<tr>';
                echo '<td>';
                echo "<a strong href='" . $filename . "'>" . $fileH . "</a>";
                echo '</td>';
                echo '<td>';
                echo $txt;
                echo '</td>';
                echo '</tr>';
                //just checking the files
                echo "$filename size " . filesize($filename) . "\n";
            }
        }
    }
}

preview


Solution

  • <?php
    
        echo '<h2>Escolha o Esquema de MetaDados a utilizar</h2>';
        echo '<table cellspacing="0" cellpadding="0" border="0" class="table">';
        echo '<tr>';
        echo '<th>Esquema de MetaDados</th>';
        echo '<th>Descrição</th>';
        echo '</tr>';
    
              foreach (glob("Templates/Dinamico/*.html") as $filename) {
              foreach(glob("Templates/Dinamico/*.txt") as $txtname){ 
               $fileH= basename($filename,".html");
               $fileT= basename($txtname,".txt");   
              if($fileH==$fileT){ 
              $txt= file_get_contents ( $txtname); 
            if($filename != "Templates/Dinamico/formD1.html"){ 
                echo'<tr>';
                echo'<td>';
                echo "<a name='".$fileH."'strong href='Templates/Dinamico/formD1.php?id=$fileH'>".$fileH."</a>";
                echo'</td>';
                echo'<td>';
                echo $txt;
                echo'</td>';
                echo'</tr>';
    
    }
    }
    }
    }
    
        ?>