get all the images from a folder in php get all the images from a folder in php wordpress wordpress

get all the images from a folder in php


try this

$directory = "mytheme/images/myimages";$images = glob($directory . "/*.jpg");foreach($images as $image){  echo $image;}


you can do it simply with PHP opendir function.

example:

$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');while($file = readdir($handle)){  if($file !== '.' && $file !== '..'){    echo '<img src="pictures/'.$file.'" border="0" />';  }}


when you want to get all image from folder then use glob() built in function which help to get all image . But when you get all then sometime need to check that all is valid so in this case this code help you. this code will also check that it is image

  $all_files = glob("mytheme/images/myimages/*.*");  for ($i=0; $i<count($all_files); $i++)    {      $image_name = $all_files[$i];      $supported_format = array('gif','jpg','jpeg','png');      $ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));      if (in_array($ext, $supported_format))          {            echo '<img src="'.$image_name .'" alt="'.$image_name.'" />'."<br /><br />";          } else {              continue;          }    }

for more information

PHP Manual