tags:

views:

52

answers:

2

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/thumb_$file></a>");
      }
   } 
   closedir($dirHandle);
?>
+2  A: 
    <?
   $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);
?>
dusoft
you are so wrong, there is check if thumb_ is in the filename, if not show it.
dusoft
btw: you just copied my solution and added it to the IF statement above
dusoft
overlooked the thumb check. sorry. reported your unjustified accusation that my answer is just a copy of yours.
jitter
i am fine with that since i suspect that because of your unjustified accusation of my code not working, your answer (posted later) was chosen over mine (posted earlier)
dusoft
A: 
  • 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);
?>
jitter
thanks! </appreciation>
Patrick