0

Hey here is what i want to do:

I have a folder "images" which contains images named like "MB_FM_Example.png".

I want to loop through the folder and echo the images. Something like that:

<?php
$files = scandir('images/');
if ($files !== false) 
{
foreach(is_array($files) as $f) {
if ($f == '..' || $f == '.')
?>
<li><img src="<?php echo 'images/'.$f ?>" alt="<?php echo $f ?>" title="">
<?php
}
} 
?>

I need the images to have an url depending on the name of the image. For example image "MB_FM_Example1.png" will get the url "Examplepage?v=FM"

How can i realize this with php?

Thanks guys

EDIT:

I need just a part of the filename like in my example "FM" and i want the images to get an url like <a href="examplepage?v=FM"> <img src="IMAGEPATH"> </a>

Marc Ster
  • 2,276
  • 6
  • 33
  • 63

2 Answers2

1

Have a look at http://php.net/glob and the related examples

Something like glob('yourpathname/') will return an array of all files in the directory, from there on you just iterate through the array to get each filename.

gts
  • 627
  • 5
  • 13
1

This is what i was searching for:

<?php
      // perform actions for each file found
      foreach (glob("images/*.png") as $filename) {
         $filenamepart = explode("_", $filename);
        echo "<a href='examplepage?v=".$filenamepart[1]."'> <img src='".$filename."'> </a>";
      }
    ?>
Marc Ster
  • 2,276
  • 6
  • 33
  • 63