greyoxide
greyoxide

Reputation: 1287

Loop through directory

I'm trying to use PHP to bring in every image from a specific directory and turn them into HTML objects by wrapping some markup around them.

Right now I am just trying to get the images on the page. In the future I would also like to bring in a text file that has the same file name as the image(slide#4.jpg and slide#4.txt).

This is what I have so far.

<?php

    $dir = "includes/pages/products/*.jpg";

    $images = glob( $dir );

    foreach( $images as $image ):
        $file = basename($image);
        $file = basename($image, ".jpg");
        echo "<div class='"prod-cont"'>" . "<h4>" . $file . "</h4>" . "<img src'" . $image "' />" . "</div>";
    endforeach;
?>

I'm not having much luck getting this going, any help is appreciated.

Upvotes: 0

Views: 80

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

Remove the second $file = line, and make sure you're generating correct HTML.

echo "<div class='prod-cont'><h4>".$file."</h4><img src='".$image."' /></div>";

Upvotes: 1

Related Questions