PHP: Pull all images from a specified directory using relative image path

Viewed 1917

I am trying to create a gallery where images are automatically pulled from a images directory.

When I foreach() each image it returns however it pulls the entire root path as the image src

/home/dev/public_html/assets/images/

this causes the images not to show and shows dead linked images on screen. I need the relative path like this:

/assets/images/imagename.jpg

How would I set a base path/URL?

$dirname = "/home/dev/public_html/assets/images/";                              

$images = glob($dirname."*.jpg");

foreach($images as $image) {
    echo '<img src="'.$image.'" /><br />';
}

I tried setting $dirname to this

$dirname = "/assets/images/"; 

But I get no result and nothing pulls through at all.

The file i am creating this in, is located outside the public_html folder, i believe this could be casing the issue but the file cannot be relocated.

2 Answers
Related