Post Images From A Directory Dynamically

Hello All,

What I’d like to do is grab all images from a folder and post them dynamically to a web page. This code works fine if I hardcode in the exact folder, but the tricky part is getting it to work for all folders by looking at the URL and stripping it of the .php extension, which always matches the folder name exactly (a variable I have called $topic).

Does anyone have any ideas how to get this to work? I am fairly new to programming in general so I apologize if this isn’t very clear.

Here is the code:


$topic = basename($_SERVER[‘PHP_SELF’],“.php”).PHP_EOL;
$files = glob(‘img/’ . $topic . ‘/.’);

for ($i=0; $i<count($files); $i++) {
$num = $files[$i];
echo ‘<a href="’.$num.‘" target=“_blank”>’
.‘<img class=“thumb” src="’.$num.‘">’
.'</a>  ';


This gallery demo may be of some help as a starting point.

I don’t think you need PHP_EOL appended to the $topic though, that just adds an OS independent line end.

Yep, you’re right. Thanks!

Thanks Sogo! I wasn’t able to get the sample code to work, but it is a good starting point.