The following block of code is 100% working. The problem is that every time I refresh the page it just adds on to what it had previously wrote. This just makes it repeat the information in the container div. I tried to use replaceChild instead of appendChild but all attempts to adjust the code results in failure. I would be grateful if someone could help me make this code replace the content that is between the container div tags instead of adding on to it.
<?php
$file = "./test.html";
$imgs = glob("1/*.*");
$dom = new DOMDocument();
$dom->loadHTMLFile($file);
$container = $dom->getElementByID("container");
if(count($imgs) == 0 || $imgs === false) die('Error: glob is failing.');
foreach($imgs as $img)
{
$basename = pathinfo($img, PATHINFO_BASENAME);
$tag = $dom->createElement("img");
$tag->setAttribute('src', '1/'.$basename);
$container->appendChild($tag);
$tag = null;
}
$dom->saveHTMLFile($file);
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="container"></div>
</body>
</html>