46

How to display the image from a specified directory? like i want to display all the png images from a directory, in my case my directory is media/images/iconized.

I tried to look around but seems none of them fits what i really needed.

But here's my try.

$dirname = "media/images/iconized/";
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
    if(!in_array($curimg, $ignore)) {
        echo "<img src='media/images/iconized/$curimg' /><br>\n";
    }
}

hope someone here could help. Im open in any ideas, recommendation and suggestion, thank you.

Martijn
  • 15,791
  • 4
  • 36
  • 68
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • 1
    What isn't working with your try? You need to provide more information about the issues you are having. – webnoob Aug 10 '12 at 14:10
  • This code works for me. One can only assume you had a folder path that didn't resolve correctly. Or the folder wasn't readable by the script. – Progrock Nov 19 '18 at 04:27
  • that would be more great if we try to display image with a timer functionality. – Muhammad Ali Sep 20 '21 at 05:14

6 Answers6

101

You can also use glob for this:

$dirname = "media/images/iconized/";
$images = glob($dirname."*.png");

foreach($images as $image) {
    echo '<img src="'.$image.'" /><br />';
}
Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
Loken Makwana
  • 3,788
  • 2
  • 21
  • 14
  • how about multiple image extension, like instead of only png's, i want to be consider the JPEG too or gif or as what i want. – Juliver Galleto Aug 10 '12 at 14:27
  • 4
    you can write like this glob("{$dirname}*.png, {$dirname}*.jpeg, {$dirname}*..gif") – Loken Makwana Aug 13 '12 at 03:07
  • 9
    @MightyLucene What you are suggesting in the comment is not working. A way that woks is this: `$images = glob($dirname."*.{jpg,gif,png}",GLOB_BRACE);` From `glob` manual : GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c' – Segmentation Nov 09 '16 at 22:04
  • multiple extensions go like this: `$images = glob($dirname."*.[JjPp][PpNn][Gg]");` So just fill each character with its possible combinations for a quick and okay implementation – mewc Oct 24 '17 at 23:17
  • `$dirname` can also be used with wildcards: `"media/images/*/"` or even `"media/images/*/*/"`... – lepe Mar 02 '18 at 08:25
  • 1
    I suggest to add limits to the image: `echo " ";` – lepe Mar 02 '18 at 08:29
21

You can display all image from a folder using simple php script. Suppose folder name “images” and put some image in this folder and then use any text editor and paste this code and run this script. This is php code

    <?php
     $files = glob("images/*.*");
     for ($i=0; $i<count($files); $i++)
      {
        $image = $files[$i];
        $supported_file = array(
                'gif',
                'jpg',
                'jpeg',
                'png'
         );

         $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
         if (in_array($ext, $supported_file)) {
            echo basename($image)."<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
             echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
            } else {
                continue;
            }
          }
       ?>

if you do not check image type then use this code

<?php
$files = glob("images/*.*");
for ($i = 0; $i < count($files); $i++) {
    $image = $files[$i];
    echo basename($image) . "<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
    echo '<img src="' . $image . '" alt="Random image" />' . "<br /><br />";

}
?>
Sufyan Shaik
  • 23
  • 1
  • 5
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
  • I believe this code assumes that every file in images folder is actually an image. If a text file is there by a mistake then it would get displayed as well. – Savas Vedova Oct 07 '13 at 05:59
  • 1
    Why start at 1 in the for loop? isn't the first file being forgotten then? – Rafael Jul 24 '15 at 16:23
  • i just use a loop so i can start from 0 or 1 and i also use count(array) so loop continue all image from folder – Shafiqul Islam Jul 25 '15 at 08:50
  • All files, not all images. if you intent to put it like this then all extensions are accepted. including .pdf, .txt etc. – Rafael Aug 14 '15 at 09:45
5

You need to change the loop from for ($i=1; $i<count($files); $i++) to for ($i=0; $i<count($files); $i++):

So the correct code is

<?php
$files = glob("images/*.*");

for ($i=0; $i<count($files); $i++) {
    $image = $files[$i];
    print $image ."<br />";
    echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
}

?>
Bucket
  • 7,415
  • 9
  • 35
  • 45
Belal Almassri
  • 119
  • 1
  • 8
2

In case anyone is looking for recursive.

<?php

echo scanDirectoryImages("images");

/**
 * Recursively search through directory for images and display them
 * 
 * @param  array  $exts
 * @param  string $directory
 * @return string
 */
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
{
    if (substr($directory, -1) == '/') {
        $directory = substr($directory, 0, -1);
    }
    $html = '';
    if (
        is_readable($directory)
        && (file_exists($directory) || is_dir($directory))
    ) {
        $directoryList = opendir($directory);
        while($file = readdir($directoryList)) {
            if ($file != '.' && $file != '..') {
                $path = $directory . '/' . $file;
                if (is_readable($path)) {
                    if (is_dir($path)) {
                        return scanDirectoryImages($path, $exts);
                    }
                    if (
                        is_file($path)
                        && in_array(end(explode('.', end(explode('/', $path)))), $exts)
                    ) {
                        $html .= '<a href="' . $path . '"><img src="' . $path
                            . '" style="max-height:100px;max-width:100px" /></a>';
                    }
                }
            }
        }
        closedir($directoryList);
    }
    return $html;
}
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
  • Strict standards: Only variables should be passed by reference. The: && in_array(end(explode('.', end(explode('/', $path)))), $exts) part. – Rafael Jul 24 '15 at 16:19
0

Try the SPL DirectoryIterator class

<?
foreach ((new DirectoryIterator("mydir/")) as $fileinfo) {
   // Ignore .files (.htaccess, .DS_Store, etc)
   if (!$fileinfo->isDot()) {
      // Check if file is a PNG
      if ($fileinfo->getExtension() == 'png') {
         echo "This is a PNG image.";
      }
   }
}
WLFree
  • 119
  • 10
-2

Strict Standards: Only variables should be passed by reference in /home/aadarshi/public_html/----------/upload/view.php on line 32

and the code is:

<?php

echo scanDirectoryImages("uploads");

/**
* Recursively search through directory for images and display them
* 
* @param  array  $exts
* @param  string $directory
* @return string
*/
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
{
if (substr($directory, -1) == '/') {
    $directory = substr($directory, 0, -1);
}
$html = '';
if (
    is_readable($directory)
    && (file_exists($directory) || is_dir($directory))
) {
    $directoryList = opendir($directory);
    while($file = readdir($directoryList)) {
        if ($file != '.' && $file != '..') {
            $path = $directory . '/' . $file;
            if (is_readable($path)) {
                if (is_dir($path)) {
                    return scanDirectoryImages($path, $exts);
                }
                if (
                    is_file($path)
                    && in_array(end(explode('.', end(explode('/', $path)))),   $exts)
                ) {
                    $html .= '<a href="' . $path . '"><img src="' . $path
                        . '" style="max-height:100px;max-width:100px" />  </a>';
                }
            }
        }
    }
    closedir($directoryList);
}
return $html;
}