displaying only thumbnails in directory, but linking to full size image
I have a directory with fullsize images and thumbnails. The thumbnails are prefixed with thumb_
and then share the same name as the full size counterparts.
What do i need to do the script below to get both the full image and the thumb, so i can echo the correct link? As is, it returns all images.
<?
$dirHandle = opendir("images");
while ($file = readdir($dirHandle)) {
if(!is_dir($file) && strpos($file, '.jpg')>0 || strpos($file, '.gif')>0 || strpos($file, '.png')>0) {
echo ("<a href=images/$file><img src=images/thu开发者_StackOverflowmb_$file></a>");
}
}
closedir($dirHandle);
?>
<?
$dirHandle = opendir("images");
while ($file = readdir($dirHandle)) {
if(!is_dir($file) && strpos($file, '.jpg')>0 || strpos($file, '.gif')>0 || strpos($file, '.png')>0) {
if (strpos($file,"thumb_")===FALSE) echo ("<a href=images/$file><img src=images/thumb_$file></a>");
}
}
closedir($dirHandle);
?>
- only do something if you don't have a thumbnail
&& stripos($file, 'thumb_') === false
- create thumbnail-path directly as it is known from normal filename
This should work (warning: 1min code sling)
<?
$dirHandle = opendir("images");
while ($file = readdir($dirHandle)) {
if(!is_dir($file) && (strpos($file, '.jpg')>0 || strpos($file, '.gif')>0 || strpos($file, '.png')>0) && stripos($file, 'thumb_') === false) {
echo ("<a href=images/$file><img src=images/thumb_$file></a>");
}
}
closedir($dirHandle);
?>
精彩评论