Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull all images from a specified directory and then display them

Tags:

php

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.

like image 659
Juliver Galleto Avatar asked Aug 10 '12 14:08

Juliver Galleto


People also ask

How do I display all images in a directory in HTML?

Simply run the command from a command line window in the directory where your images are stored. If you need to have the all. html in some other place either move it there or change to >> C:\files\html\all.

How can I get a list of all the subfolders and files present in a directory using PHP?

PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.


2 Answers

You can also use glob for this:

$dirname = "media/images/iconized/"; $images = glob($dirname."*.png");  foreach($images as $image) {     echo '<img src="'.$image.'" /><br />'; } 
like image 170
Loken Makwana Avatar answered Sep 28 '22 09:09

Loken Makwana


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 />";  } ?> 
like image 35
Shafiqul Islam Avatar answered Sep 28 '22 09:09

Shafiqul Islam