SK0004
SK0004

Reputation: 71

Showing Images in PHP

I want to show images residing inside a directory in PHP The code which I wrote was

<?php

$dir = opendir("upload");


while (($file = readdir($dir)) !== false)
  {
  echo "filename: " . $file . "<br />";

  echo "<a href='shoe.php?image_name=".$file."'> <img src='upload/".$file[0]."' width='150' height='150' /></a></td>";

  }
closedir($dir);
?> 

Here the images are not displayed. Only a frame comes for that image and the actual image is not displayed inside that. Where I have went wrong? Can any one please help...

Upvotes: 0

Views: 346

Answers (3)

bicccio
bicccio

Reputation: 394

why $file[0]??? $file is correct:

<?php

        $dir = opendir("upload");

        while (($file = readdir($dir)) !== false)
          {
          echo "filename: " . $file . "<br />";

          echo "<a href='shoe.php?image_name=".$file."'> <img src='upload/".$file."' width='150' height='150' /></a>";

          }
        closedir($dir);
    ?>

Upvotes: 0

amosrivera
amosrivera

Reputation: 26514

PHP Glob makes it much easier to list directories. You can do something like

$image = glob("*.jpg")
foreach ($image as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}

Upvotes: 1

user142162
user142162

Reputation:

Replace $file[0] with $file, as $file is not an array and that will only return the first character of the string.

Upvotes: 0

Related Questions