Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cand I insert all images in HTML from a folder with a single line of code?

Tags:

html

I have folder img and inside of the folder I have 200 pictures. Do I have any possibility to insert all the images in a html pages to be shown, without writing this line of code 200 times?

<img src="img/ladybug.jpg" alt="Ladybug" title="Ut rutrum, lectus eu pulvinar elementum, lacus urna vestibulum ipsum"></li>
like image 806
Peter Avatar asked Jan 30 '26 05:01

Peter


2 Answers

You have to use server side language such as PHP or ASP

PHP example:

$dir    = '/images';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
  if(fnmatch('*.jpg',$file)) {
    $images[] = $file;
  }
}

foreach($images as $image) {
  echo '<img src="images/'.$image.'" />';
}
like image 169
Alex Rashkov Avatar answered Jan 31 '26 19:01

Alex Rashkov


If you think you can get away with a simple line of html, then no, that's a no-go.

I guess theoretically you could do it client side, by parsing the directory listing returned by the webserver, but that's really way to complex to handle this.

The only reasonable way is to do it server side using php or the likes. The problem you'll be stuck with though, is that a pure directory listing doesn't contain enough information to get values for alt and title (which is what Bernd was getting at).

I suppose with some nifty php, you could get those out of exif though.

like image 22
Joeri Hendrickx Avatar answered Jan 31 '26 19:01

Joeri Hendrickx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!