Aggregating HTML files I am using PHP to keep my webpage organized.

The directory looks like this:

root/folder/file1
root/folder/file2
root/folder/file3
root/index.php


Each file is very simple html doc with content containing a title tag, which I use to create a nav bar.

I want index.php to aggregate all the content from file1, file2 and file3, in addition to creating a nav bar up at the top.

The code I have achieves this successfully, but I'm not sure if I'm doing this in a practical way.

$domain = "domain/"
$dir = "folder/";
$files = scandir($dir);

// loop over all the files, saving their contents and ids to arrays
$contents = Array();
$titles = Array();
$ids = Array();
for ($x = 0; $x &lt; sizeof($files); $x++) {   
  if ($files[$x] != "." &amp;&amp; $files[$x] != "..") {
    $filename = $domain . $dir . $files[$x];
    $file = fopen($filename, "r");
    $data = file_get_contents($filename);
    array_push($contents, $data);

    $regex = '#&lt;title&gt;(.*?)&lt;\/title&gt;#';  
    preg_match($regex, $data, $match);
    $title = $match[1];
    array_push($titles, $title);

    $regex2 = '#( |-|,)#';
    $id = preg_split($regex2, $title)[0];   
    array_push($ids, $id);
  }
}

// the first loop sets the nav bar
echo "&lt;div id='navigation'&gt;";
echo "&lt;ul&gt;";
for ($x = 0; $x &lt; sizeof($contents); $x++) {
    echo "&lt;li&gt;&lt;a href='#" . $ids[$x] . "'&gt;" . $titles[$x] . "&lt;/a&gt;&lt;/li&gt;";    
}
echo "&lt;/ul&gt;";
echo "&lt;/div&gt;";

// this second loop sets the contents
echo "&lt;div id='content'&gt;";
for ($x = 0; $x &lt; sizeof($contents); $x++) {
    echo "&lt;div id='" . $ids[$x] . "'&gt;" . $contents[$x] . "&lt;/div&gt;";    
}
echo "&lt;/div&gt;";


I like the idea of having all the content on one big page and the nav bar helps a lot when I'm viewing it on my phone.

Questions


Are there any obvious problems with the way I am aggregating my files?
Are there challenges I may encounter that I have not yet experienced?
Is there a generally accepted way of doing this while achieving the same result?
Basically you need to remove the unused fopen() call and change ponderous operators to handy ones as for some reason you made a peculiar choice in favor of the former, like for vs. foreach, array_push vs simple assignment, etc. Also consider a cleaner way to output HTML 

&lt;?php
$pattern = "domain/folder/*.*";
$files = glob($pattern);

// loop over all the files, saving their contents and ids to arrays

$contents = [];
$titles = [];
$ids = [];
foreach ($files as $filename)
{
    $data = file_get_contents($filename);
    $contents[] = $data;

    preg_match('#&lt;title&gt;(.*?)&lt;\/title&gt;#', $data, $match);
    $titles[] = $match[1];

    $ids[] = preg_split('#( |-|,)#', $match[1])[0];
}
?&gt;
&lt;div id='navigation'&gt;
&lt;ul&gt;
&lt;?php foreach ($titles as $x =&gt; $title): ?&gt;
    &lt;li&gt;&lt;a href="#&lt;?=$ids[$x]?&gt;"&gt;&lt;?=$title?&gt;&lt;/a&gt;&lt;/li&gt;
&lt;?php endforeach ?&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;div id='content'&gt;
&lt;?foreach ($contents as $x =&gt; $content): ?&gt;
    &lt;div id="&lt;?=$ids[$x]?&gt;"&gt;&lt;?=$content?&gt;&lt;/div&gt;
&lt;?php endforeach ?&gt;
&lt;/div&gt;