0

In first I have original images. And in the second folder I have thumbs with the same name. I want to show thumbs and link to original. How can I do it?

<html><?php 
      $folder = "img";
      $img_array = glob("$folder/*.*");

      arsort($img_array);
      if (!count($img_array)) { 
        echo"ERROR - no images in folder!"; die;  
      } 

    foreach($img_array as $key => $value) {    
    ?> 
            <a href="<?php echo $value; ?>"><img src="<?php SOMETHING THERE ?>" /></a>
    <?php } ?> 
</html>

Folder with original images: img

Folder with thumbs: img/thumbs

I want:

<a href="img/image1.jpg"><img src="img/thumbs/image1.jpg" /></a>
Karol
  • 155
  • 1
  • 2
  • 11

3 Answers3

1

Supposing your img folder is in the document root of your HTTP server:

<html>
<?php 
      $folder = "img";
      $img_array = glob("$folder/*.*");

      arsort($img_array);
      if (!count($img_array)) { 
          echo "ERROR - no images in folder!"; die;  
      } 

      foreach($img_array as $value) { 
          $name = basename($value);
          echo "<a href='/img/$name'><img src='/img/thumbs/$name' /></a>";
      } 
?> 
</html>
evalarezo
  • 1,134
  • 7
  • 13
0

In the foreach, have an

<img src="img/" . "$imgName" />

and

<img src="img/thumbs/" . "$imgName" />

Then it will first show the image in the img folder, then the same image in the img/thumbs folder.

So the foreach will be something like this:

foreach($img_array as $key => $value) {    
    print("

        <a href='yourHTMLPage.html'><img src='img/' . '$value'></a>
        <a href='yourHTMLPage.html'><img src='img/thumbs/' . '$value'></a>

    ");
}
Jeroen
  • 2,011
  • 2
  • 26
  • 50
0

Maybe I completely misunderstood it, but I think JeroenJKs answer doesn't fit.

You create a link with the <a>-Tag and instead of a text, you use your image.

<a href="<?php echo $value;?>"><img src="thumbs/<?php echo $value?>" /></a>

Seems almost too simple to me :o

Sbls
  • 495
  • 1
  • 4
  • 10
  • try print $value and than you see that your answer is bad too – Karol Jan 04 '14 at 18:13
  • @Karol: I have no idea what you are trying to say. Why should printing $value be bad? You even did it in your code. – Sbls Jan 04 '14 at 18:15
  • printing $value: ` img/image1.jpg` ` img/image2.jpg` ` img/image3.jpg` – Karol Jan 04 '14 at 18:16
  • and that's not what you want? – Sbls Jan 04 '14 at 18:19
  • than you get: its bad i want – Karol Jan 04 '14 at 18:21
  • Then just delete the img I made in both href and src. Did you even look into the code? – Sbls Jan 04 '14 at 18:23
  • I'm not agree with your answer for many reasons. First, [is not remomended to use the short output tag](http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use), and second and most important, the URLs are wrong because the [glob function](http://www.php.net/manual/es/function.glob.php) return the directory too. – evalarezo Jan 04 '14 at 18:29
  • I am a lazy bitch. But yes, you are right. Especially when I'm trying to explain something. Fixed it. – Sbls Jan 04 '14 at 18:38