0

Simple question that I'm not sure how to ask google:

If I have a folder for each user on my website, and each folder has only 1 jpg picture in it ( along with other different, currently unimportant files ),

Is there an easy way to display that img without actually specifying the name of the jpg file? eg.

<img src="http://www.domain.com/users/315/*.jpg>

Or would I have to use php scandir on the directory each time to get the specific name?

Sorry if duplicate, I wasn't able to find an answer elsewhere and wasn't sure how to actually search this. Thanks.

user3817799
  • 147
  • 1
  • 7
  • 1
    Have you just simply google'd your title ?! – Rizier123 Jul 31 '15 at 18:32
  • http://stackoverflow.com/a/12896374/4627253 – Miguel Jul 31 '15 at 18:35
  • @Rizier123 yes...I have...However I only seem to find answers that describe how to show multiple images from a folder. I'm interested in finding out if its possible to call THE ONLY image of a folder in a similar way to the one in my example. – user3817799 Jul 31 '15 at 18:47
  • 1
    @user3817799 If you search for all images and you only find one, then one == all images – Rizier123 Jul 31 '15 at 18:48
  • @Rizier123 Thanks, I get that, but the reason I wanted to know if there was such a simple way, is because I intend on having a page with lets say 100 different user's pictures in various places. So to me, the simplest thing would've been to have a simple img tag where the only thing I'd have to change is the directory ( 315 in the example) which relates to the user's ID – user3817799 Jul 31 '15 at 18:54
  • `list($jpg) = glob("*.jpg"); echo $jpg;` – Salman A Aug 03 '15 at 19:00

1 Answers1

1

I'd say use scandir (add the relative path to your directory is instead of "img"):

<?php
$dir = 'img';
$file = scandir($dir, 1);
?>

<img src="img/<?= $file[0]; ?>">

More on scandir here.

Juan Serrats
  • 1,358
  • 5
  • 24
  • 30