Output images in HTML form using PHP

Hello.
I do the following:

$dir = "images/";
$odir = opendir($dir);
while ($file = readdir($odir))
{
    $word = explode("#", $file);
$big = $dir."big#".$word[1];
$small = $dir."small#".$word[1];
    echo "<a href=".$big." target=_blank><img src=".$small." hspace=30px vspace=20px></img></a>";
}
closedir($odir);

to display pictures on the page. pictures are not displayed.
Please tell me where I’m wrong?


Answer 1

Here’s your answer:

<?php 
$dir = "./images/";
$odir = opendir($dir);
while ($file = readdir($odir))
{
    if($file == ".." || $file == ".")
    continue;
    $word = explode("-", $file);
    $big = $dir."big-".$word[1];
    $small = $dir."small-".$word[1];
    echo "<a href=\"".$big."\" target=_blank><img src=\"".$small."\" hspace=30px vspace=20px></a>";
}
closedir($odir);

The browser does not like the “#” character. When you change to “-“, everything works.
In addition, you now need to somehow figure out what to do with large and small pictures, otherwise you will have two pieces.
For example:

...
$word = explode("-", $file);
if($word[0] == "small")
   continue;
...

This is how I would do it offhand.

Leave a reply

Please enter your comment!
Please enter your name here