conor909
conor909

Reputation: 1623

using 'glob' to get all images from a directory

I'm trying to get all images from a directory and print them. I know my URL is correct because I am echoing the the $dirname along with a .jpg image I have saved in the directory. What am I doing wrong?

  <?php

  $dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
  $images = glob($dirname . "*.jpg");
  foreach($images as $image) {
  echo '<img src="'.$image.'"/>';
  }

  //check to see if $dirname is correct

  echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';

  ?>

//edit

I have solved my problem, by removing the jpg extention and just pulling "*" for everything in the folder. Although it works for me now, its not best practice. It must be something to do with different .jpg file formats?

Upvotes: 0

Views: 3375

Answers (2)

RedstoneDwarf
RedstoneDwarf

Reputation: 126

You used lowercase file extension here.

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

And uppercase JPG in your test output.

  echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';

If you're using a linux system then file extensions are case sensitive.

Upvotes: 1

Tularis
Tularis

Reputation: 1506

You need to prepend the dirname to glob's result to get the full paths.

So something like:

$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
   echo '<img src="'.$dirname.$image.'"/>';
}

Upvotes: 0

Related Questions