问题
I can not find an answer(that works) to this anywhere on the web.
I am trying to get Lightbox to load images from a directory as they will be frequently updated.
If anyone can correct what I'm doing wrong, or has a solution using either PHP or a hidden div populated automatically by a specific directory it would be greatly appreciated.
Here is what I have come up with but is not seeming to work;
<?php $dirname = "img/love/"; $images = glob($dirname."*.jpg"); foreach($images as $image) { echo '<img data-lightbox="love" src="'.$image.'" /><br />'; } ?>
and here is my test page: http://knowledgeoverfame.com/tslp/leet/alt/index.html
I didn't find any similar questions here with an answer to this but if i may have missed it please enlighten me :)
Thanks!
回答1:
Try using scandir()
to get an array of your image files.
Example:
<?php
$images = scandir($your_folder);
foreach ( $images as $key => $filename ) {
if ( $key > 1 ) { //ignores the first two values which refer to the current and parent directory
echo '<img data-lightbox="love" src="'.$your_folder."/".$filename.'" /><br />';
}
}
?>
for reference: PHP scandir()
回答2:
What you want to do - use lightbox to display images from a folder - is a problem which has been solved lots of times with varying degrees of complexity. If you do a google search for something like "php image gallery" you will find sample scripts. I did just this about a year ago and after some experimentation I chose Minigal Nano as it was a powerful script but simple enough for it to be quite easy to see how it worked and then rework for my own use. I also found this an effective way to learn php.
回答3:
This script will pull the images from a directory. I used it with fancybox but you can modify to fit your needs.
<?php
$directory = 'yourDirectory/gallery/thumbs'; //where the gallery thumbnail images are located
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE);
natsort($files); //sort by filename
?>
<?php
for($x = 0; $x < count($files); $x++){
$thumb = $files[$x];
$file = basename($thumb);
$nomargin = $x%4 == 0?" nomargin":"";
$title = htmlspecialchars($file);
?>
<div class="thumbs fancybox<?= $nomargin ?>" style="background:url('<?= $thumb ?>') no-repeat 50% 50%;">
<a rel="group" href="yourDirectory/gallery/<?= $file ?>"></a>
</div>
<?php
}//end for loop
?>
来源:https://stackoverflow.com/questions/25540534/lightbox-display-multiple-images-from-directory